admin管理员组文章数量:1406016
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]
}
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]
}
Share
Improve this question
edited Mar 18, 2016 at 7:22
user663031
asked Mar 18, 2016 at 4:24
Mohamed HegazyMohamed Hegazy
2734 silver badges18 bronze badges
5
-
3
arr = arr.filter(e => e);
– Tushar Commented Mar 18, 2016 at 4:24 -
2
arr = arr.filter(Boolean)
is built-in ES5. – dandavis Commented Mar 18, 2016 at 4:25 - Possibly duplicate stackoverflow./questions/31925323/… – brk Commented Mar 18, 2016 at 4:41
-
Where did you find the syntax
x == (a || b || c || d)
and what do you expect it to do? Why are you trying to usesplice
instead of usingfilter
to create a new array? – user663031 Commented Mar 18, 2016 at 4:53 - I was trying to check if x is equal to a or b or c or d, I can tell from your ment that it's wrong syntax, would you please explain why? I was using splice because I am new to programming so I didn't know any better – Mohamed Hegazy Commented Mar 23, 2016 at 3:10
5 Answers
Reset to default 3Problem in code:
- Iterate over array from last element to first element
Array#splice
expects first argument as index, not the item itself- To check if an element is
NaN
, useisNaN()
. - To pare with multiple values, pare the elements individually with each item
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 ment
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));
jsfiddle
use lodash.
console.log(_.pact(arr));
You refer to using splice
so I assume you want an in-place (mutating) solution. Here's a simple, quick approach:
function pact(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./pacting-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 pact(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
本文标签:
版权声明:本文标题:how to remove null, undefined, NaN, 0s, falses and empty string from an array in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744372748a2603110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论