[英]How does one remove number values and empty values from an array?
As part of my nightwatchjs testing, I have a dynamic array (in an object) that requires some tweaking.作为我 nightwatchjs 测试的一部分,我有一个动态数组(在一个对象中),需要一些调整。
My current array looks like this;我当前的数组看起来像这样;
{ GenericColour: [ 'Black',
5059,
'White',
3610,
'Grey',
3281,
'Blue',
2131,
'Silver',
1408,
'Red',
1190,
'',
491,
'Yellow',
59,
'Green',
50,
'Orange',
31 ] }
but with it being dynamic, it might look slightly different the next time I run the test (a different colour might be added, or a current colour no longer listed, etc.).但由于它是动态的,下次我运行测试时它可能看起来略有不同(可能会添加不同的颜色,或者不再列出当前颜色等)。
What I would like to try and do is remove all the numbers, extra commas, etc. so I'm just left with an array reading (as per the example above);我想尝试做的是删除所有数字、多余的逗号等,所以我只剩下一个数组读数(根据上面的例子);
['Black','White','Grey','Blue','Silver','Red','Yellow','Green','Orange']
Is there a way of doing this, either using JavaScript commands or regex?有没有办法做到这一点,或者使用 JavaScript 命令或正则表达式?
You can use Array#filter
, using the typeof
operator to check if the element is a string.您可以使用
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);
You do that with the filter function:您可以使用过滤器 function:
obj = obj.GenericColour.filter(value => typeof value === 'string' )
int numberArry = []
arry.forEach(value => if(isNaN(value)) numberArry.push(value));
You can use array.prototype.filter to check for items in the array that are strings, removing all other items eg numbers.您可以使用 array.prototype.filter 来检查数组中的字符串项目,删除所有其他项目,例如数字。 Check the link below for more info.
检查下面的链接以获取更多信息。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
it can be something like this:它可以是这样的:
array.filter((item) => typeof item === "string");
- this will return a new array with only the strings in the current array: - 这将返回一个新数组,其中仅包含当前数组中的字符串:
['Black','White','Grey','Blue','Silver','Red','Yellow','Green','Orange']
Simplest way would be passing isNaN to the filter method.最简单的方法是将 isNaN 传递给 filter 方法。
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);
One way is to filter
based on checking length
propterty.一种方法是基于检查
length
属性进行filter
。
For numbers and ''
, the length value is 0 (false).对于数字和
''
,长度值为 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);
There are cases, where one can not reassign a filtered result to the original source reference.在某些情况下,无法将过滤结果重新分配给原始源参考。 Like with...
就像...
const arr = [1, 2, 3]; arr = arr.filter(/* ... */);
... which will fail. ...这将失败。 One also might need to deal with properties which are not allowed to be written but of cause still can be mutated.
还可能需要处理不允许写入但原因仍然可以变异的属性。
Then one needs a mutating remove approach based on a callback, exactly of the kind that filter
asks for, where this callback function is the condition of whether to remove an array item or not.然后需要一种基于回调的变异删除方法,正是
filter
要求的那种,其中这个回调 function 是是否删除数组项的条件。
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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.