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.
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 NaN
, use 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
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
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>';
This solution may not be that efficient
function filterArray(el) {
return (typeof (el) === "number" && el != 0 && !isNaN(el));
}
arr = [NaN, 0, 15, false, -22, '', undefined, 47, null];
console.log(arr.filter(filterArray));
使用lodash 。
console.log(_.compact(arr));
You refer to using splice
so I assume you want an in-place (mutating) solution. 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
. See 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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.