[英]how to remove null, undefined, NaN, 0s, falses and empty string from an array in JavaScript?
I wrote a script to remove any null, undefined, 0, false or empty string value using .splice(), but the code has only removed NaN and 0s.我编写了一个脚本来使用 .splice() 删除任何 null、undefined、0、false 或空字符串值,但代码只删除了 NaN 和 0。
Here is what I tried:这是我尝试过的:
function remove(arr){ // input [NaN, 0, 15, false, -22, '',undefined, 47, null]
var bin = [];
for (var i =0; i<arr.length; i++){
if (arr[i] == (NaN || 0 || false || "" || undefined || null)){
arr.splice(arr[i],1);
}
}
console.log(arr); // Expected output [15, -22, 47]
}
Problem in code:代码中的问题:
Array#splice
expects first argument as index, not the item itself Array#splice
需要第一个参数作为索引,而不是项目本身NaN
, use isNaN()
.要检查元素是否为NaN
,请使用isNaN()
。Working code:工作代码:
function remove(arr) { for (var i = arr.length - 1; i >= 0; i--) { if (isNaN(arr[i]) || arr[i] === 0 || arr[i] === false || arr[i] === "" || arr[i] === undefined || arr[i] === null) { // Which is same as // if (!arr[i]) { arr.splice(i, 1); } } console.log(arr); // Expected output [15, -22, 47] } remove([NaN, 0, 15, false, -22, '', undefined, 47, null]);
Use Array#filter
使用Array#filter
arr = arr.filter(e => e);
var input = [NaN, 0, 15, false, -22, '',undefined, 47, null]; var filteredArr = input.filter(e => e); document.body.innerHTML = '<pre>' + JSON.stringify(filteredArr, 0, 4) + '</pre>';
Or as said by @dandavis in comment或者正如@dandavis在评论中所说
arr = arr.filter(Boolean);
var input = [NaN, 0, 15, false, -22, '',undefined, 47, null]; var filteredArr = input.filter(Boolean); document.body.innerHTML = '<pre>' + JSON.stringify(filteredArr, 0, 4) + '</pre>';
使用lodash 。
console.log(_.compact(arr));
You refer to using splice
so I assume you want an in-place (mutating) solution.您指的是使用splice
所以我假设您想要一个就地(变异)解决方案。 Here's a simple, quick approach:这是一个简单快捷的方法:
function compact(a) {
var i = 0, j = 0;
while (i < a.length) {
var v = a[i++];
if (v) a[j++] = v;
}
a.length = j;
}
In case you care (and you probably shouldn't), this is 10 times faster than a loop over splice
.如果你关心(你可能不应该关心),这比splice
的循环快 10 倍。 See http://jsperf.com/compacting-an-array .请参阅http://jsperf.com/compacting-an-array 。
If you want a solution which creates a new array, the alternatives given in other answers using filter
are fine, but here's a basic version:如果您想要一个创建新数组的解决方案,使用filter
其他答案中给出的替代方案很好,但这是一个基本版本:
function compact(a) {
var i = 0, j = 0, result = [];
while (i < a.length) {
var v = a[i++];
if (v) result[j++] = v;
}
return result;
}
function bouncer(arr) {
var newArr=arr.slice(0);
var n=0;
for(var i=0; i<arr.length;i++){
if(arr[i]==false||arr[i]==null||arr[i]==0||arr[i]==""||arr[i]==undefined||Number.isNaN(arr[i])==true){
newArr.splice(n,1);
} else n++;
}
console.log(newArr);
return newArr;
}
bouncer([null, NaN, 1, 2, undefined,0,'a']);
I have tried to do it with slice()... For NaN I used the function Number.isNaN() and if returns true goes on... The input was [null, NaN, 1, 2, undefined,0,'a'] and returned [ 1, 2, 'a'] as it is expected... Hope it helped you我试过用 slice() 来做到这一点......对于 NaN 我使用了函数 Number.isNaN() 并且如果返回 true 继续......输入是 [null, NaN, 1, 2, undefined,0,' a'] 并按预期返回 [ 1, 2, 'a'] ... 希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.