0

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.

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. 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 && Commented Apr 23, 2013 at 8:42
  • which values are supposed to be null or empty? what are you expecting? Commented Apr 23, 2013 at 8:42
  • Probably you should just replace || with && 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! Commented Apr 23, 2013 at 9:05

5 Answers 5

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,,, 
Sign up to request clarification or add additional context in comments.

1 Comment

I think you mean colors[i] !== undefined it should not be 'undefined' that will make it a string
2

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

Comments

1

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.

Comments

0

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;});

Comments

0
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"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.