简单地从数组中删除空值 - javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16164699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:31:36  来源:igfitidea点击:

Remove empty values from array simply - javascript

javascript

提问by UllerUller

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.

我原以为我的 if 语句会过滤所有具有值的元素并推送到我的新数组。我真的需要 newArray 长度等于 3 并且只保存 vales,""newArray 中不应该有空字符串。

Thank you in advance.

先感谢您。

回答by Bojan Kovacevic

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,,, 

回答by shishirmk

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

回答by shakib

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

您可以简单地使用颜色 [i] 进行存在检查,

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

相关资源javascript类型转换

hope this helps.

希望这可以帮助。

回答by 3y3

If 'false' value is important then:

如果 'false' 值很重要,那么:

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

回答by Khalizar

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