admin管理员组

文章数量:1300202

I have looked at previous Q/A and I did not find much help there. Mainly as I did not understand what was coded.

I am just looking to remove any empty values in my array.

My simple approach - that is not working!

My code is -

var colors = [a, b, c, d, e, f];
var newArray = [];
for (var i = 0; i < colors.length; i++) {
  if (colors[i] !== 'undefined' || colors[i] !== null || colors[i] !== "") {
    newArray.push(colors[i]);
  }
}
console.log(newArray.length); // == 6 
console.log(newArray) //== yellow,blue,red,,,

I would have thought that my if statement would filter all elements with value and push on to my new array. I really need newArray length to equal 3 and just hold vales, no empty string "" should be in the newArray.

Thank you in advance.

I have looked at previous Q/A and I did not find much help there. Mainly as I did not understand what was coded.

I am just looking to remove any empty values in my array.

My simple approach - that is not working!

My code is -

var colors = [a, b, c, d, e, f];
var newArray = [];
for (var i = 0; i < colors.length; i++) {
  if (colors[i] !== 'undefined' || colors[i] !== null || colors[i] !== "") {
    newArray.push(colors[i]);
  }
}
console.log(newArray.length); // == 6 
console.log(newArray) //== yellow,blue,red,,,

I would have thought that my if statement would filter all elements with value and push on to my new array. I really need newArray length to equal 3 and just hold vales, no empty string "" should be in the newArray.

Thank you in advance.

Share Improve this question edited Apr 23, 2013 at 8:45 shishirmk 4252 silver badges14 bronze badges asked Apr 23, 2013 at 8:39 UllerUllerUllerUller 611 gold badge1 silver badge6 bronze badges 10
  • 1 "If the color is null or if the color is the empty string, include it in the result". No value can be identical to both null and "" at the same time. – Jon Commented Apr 23, 2013 at 8:42
  • 1 you use || between conditions, so if colors is not undefined you ll insert it (just example). use && – Bojan Kovacevic Commented Apr 23, 2013 at 8:42
  • which values are supposed to be null or empty? what are you expecting? – Oliver Watkins Commented Apr 23, 2013 at 8:42
  • Probably you should just replace || with && – Egor4eg Commented Apr 23, 2013 at 8:44
  • 1 What is with the downvotes? You don't vote on OPs experience, you should vote if the question is legit. He has tried, he has failed, he has shown us the codes. His assumption is wrong, which is why he came here in the first place! – Zlatko Commented Apr 23, 2013 at 9:05
 |  Show 5 more ments

5 Answers 5

Reset to default 4

use && instead of ||:

var colors = ["yellow", "","red", "blue", "", ""];
var newArray = [];
for (var i = 0; i < colors.length; i++) {
  if (colors[i] !== undefined && colors[i] !== null && colors[i] !== "") {
    newArray.push(colors[i]);
  }
 }
console.log(newArray.length); // == 3 
console.log(newArray) //== yellow,blue,red,,, 

use && instead of ||:

var colors = ["yellow", "","red", "blue", "", ""];
var newArray = [];
for (var i = 0; i < colors.length; i++) {
  if (colors[i] !== undefined && colors[i] !== null && colors[i] !== "") {
    newArray.push(colors[i]);
  }
 }
console.log(newArray.length); // == 3 
console.log(newArray) //== yellow,blue,red,,, 

For your use case you could also use

for (var i = 0; i < colors.length; i++) {
  if (colors[i]) {
    newArray.push(colors[i]);
  }
 }

This will filter out any falsy values. Falsy values include

false
0
""
null
undefined
NaN

You can simply use colors[i] to existence checking,

var colors = ["yellow", "","red", "blue", "", "", true, 1];
var newArray = [];
for (var i = 0; i < colors.length; i++) {
    if (typeof colors[i] == 'string' && colors[i]) {
        newArray.push(colors[i]);
    }
}
console.log(newArray) //["yellow", "red", "blue"]

relevant resource javascript type conversion

hope this helps.

If 'false' value is important then:

var colors = [0,1,'a',,'',null,undefined,false,true];
    colors = colors.filter(function(e){
        return (e===undefined||e===null||e==='')?false:~e;
    });

else:

var colors = [0,1,'a',,'',null,undefined,false,true];
        colors = colors.filter(function(e){return e;});
var colors = ["yellow", null, "blue", "red", undefined, 0, ""];

// es5:
var newArray = colors.filter(function(e){return !!e;});

// es6:
var newArray = colors.filter((e)=>!!e);

console.log(newArray.length); // ==> 3
console.log(newArray) // ==> ["yellow","blue","red"]

本文标签: Remove empty values from array simplyjavascriptStack Overflow