如何从数组中删除数值和空值?

How does one remove number values and empty values from an array?

作为我的 nightwatchjs 测试的一部分,我有一个动态数组(在一个对象中)需要一些调整。

我当前的数组如下所示;

{ GenericColour:    [ 'Black',
     5059,
     'White',
     3610,
     'Grey',
     3281,
     'Blue',
     2131,
     'Silver',
     1408,
     'Red',
     1190,
     '',
     491,
     'Yellow',
     59,
     'Green',
     50,
     'Orange',
     31 ] }

但由于它是动态的,下次我 运行 测试时它看起来可能会略有不同(可能会添加不同的颜色,或者不再列出当前颜色等)。

我想尝试做的是删除所有数字、多余的逗号等,这样我只剩下一个数组读数(按照上面的示例);

['Black','White','Grey','Blue','Silver','Red','Yellow','Green','Orange']

有没有办法做到这一点,使用 JavaScript 命令或正则表达式?

您可以使用Array#filter,使用typeof运算符来检查元素是否为字符串。

const arr = [ 'Black', 5059, 'White', 3610, 'Grey', 3281, 'Blue', 2131, 'Silver', 1408, 'Red', 1190, '', 491, 'Yellow', 59, 'Green', 50, 'Orange', 31 ];
const res = arr.filter(x => typeof x === 'string' && x);
console.log(res);

    int numberArry = []
    arry.forEach(value => if(isNaN(value)) numberArry.push(value));

您可以使用 array.prototype.filter 检查数组中的字符串项目,删除所有其他项目,例如数字。查看下面的 link 了解更多信息。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

可以是这样的:

array.filter((item) => typeof item === "string"); - 这将 return 一个仅包含当前数组中的字符串的新数组:

['Black','White','Grey','Blue','Silver','Red','Yellow','Green','Orange']

你用过滤器函数来做:

obj = obj.GenericColour.filter(value => typeof value === 'string' )

最简单的方法是将 isNaN 传递给过滤方法。

const a = [ 'Black', 5059, 'White', 3610, 'Grey', 3281, 'Blue', 2131, 'Silver', 1408, 'Red', 1190, '', 491, 'Yellow', 59, 'Green', 50, 'Orange', 31 ];
const b = a.filter(isNaN);
console.log(b);

一种方法是 filter 基于检查 length 属性。

对于数字和 '',长度值为 0(假)。

const data = [ '', 'Black', 5059, 'White', 3610, 'Grey', 3281, 'Blue', 2131, 'Silver', 1408, 'Red', 1190, '', 491, 'Yellow', 59, 'Green', 50, 'Orange', 31 ];

const res = data.filter(str => str.length);

console.log(res);

在某些情况下,无法将过滤后的结果重新分配给原始源引用。喜欢...

const arr = [1, 2, 3]; arr = arr.filter(/* ... */);

...这将失败。还可能需要处理不允许写入但仍然可以改变的属性。

然后需要一个基于回调的 mutating remove 方法,正是 filter 要求的那种,这个回调函数是是否删除的条件是否删除数组项。

function removeEveryMatchingItemByCondition(arr, condition, target) {
  target = (target ?? null);

  let idx = arr.length;
  const copy = Array.from(arr);

  // Processing the array from RIGHT to LEFT keeps the `idx` always in sync
  // with both related array items, the one of the mutated and also the one
  // of the unmutated version of the processed array reference.
  // Thus the `condition` always gets passed the unmutated shallow copy.

  while (idx) {
    if (arr.hasOwnProperty(--idx)) { // take a *sparse array* into account.

      // - keep processing the unmutated shallow copy by the `condition` method.
      // - arguments list ...[elm, idx, arr]... invoked within `target` context.
      if (condition.call(target, copy[idx], idx, copy)) {

        arr.splice(idx, 1); // mutate processed array.
      }
    }
  }
  return arr; // return the mutated array reference.
}


const sampleObject = {
  GenericColour: [
    'Black', 5059, 'White', 3610, 'Grey', 3281, 'Blue', 2131, 'Silver',
    1408, 'Red', 1190, '', 491, 'Yellow', 59, 'Green', 50, 'Orange', 31
  ],
};

removeEveryMatchingItemByCondition(
  sampleObject.GenericColour,
  // exactly what the OP was asking for in first place.
  (item => typeof item === 'number' || String(item).trim() === '')
);
console.log(sampleObject);

removeEveryMatchingItemByCondition(
  sampleObject.GenericColour,
  (item => typeof item === 'string')
);
console.log(sampleObject);
.as-console-wrapper { min-height: 100%!important; top: 0; }