Javascript 从javascript数组中删除空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2132030/
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
Remove null values from javascript array
提问by Krishna Priya
I am having a javascript array.
我有一个 javascript 数组。
addresses = new Array(document.client.cli_Build.value,
document.client.cli_Address.value,
document.client.cli_City.value,
document.client.cli_State.value,
document.client.cli_Postcode.value,
document.client.cli_Country.value);
document.client.cli_PostalAddress.value = addresses.join(", ");
I have to copy the content of all these array value to the postal address textarea. when i use the above join function, comma has been added for null values. How to remove this extra commas?
我必须将所有这些数组值的内容复制到邮政地址 textarea。当我使用上述连接函数时,为空值添加了逗号。如何删除这个额外的逗号?
Thanks
谢谢
回答by Gumbo
回答by Dave Brown
Another filter alternative
另一种过滤器替代品
myArray.filter(function(val){if(val)return val}).join(", ")
document.write(['this','','',,,,'is','a',,,'test'].filter(function(val){if(val)return val}).join(", "));
回答by Victor
Underscoreis a nice utility library for functional programming and list manipulation:
Underscore是一个很好的用于函数式编程和列表操作的实用程序库:
_.filter(addresses, function(val) { return val !== null; }).join(", ");
Edit: And there is a more compact way (Thanks Andrew De Andrade!):
编辑:还有一种更紧凑的方式(感谢 Andrew De Andrade!):
_.compact(addresses).join(", ");
回答by Valentin Podkamennyi
回答by Victor
document.client.cli_PostalAddress.value =
document.client.cli_PostalAddress.value.replace(/(, )+/g,", ");
Or should it be
或者应该是
document.client.cli_PostalAddress.value =
document.client.cli_PostalAddress.value
.replace(/(null, )/g,"").replace(/(, null)/g,"");
??
??
回答by DerMike
One could also use Array.prototype.reduce().
也可以使用Array.prototype.reduce()。
It reducesan array to a single value, e.g. a string. Like so:
它将数组缩减为单个值,例如字符串。像这样:
addresses.reduce(function (a, b) {
if (a && b) { return a + ', ' + b; }
if (a) { return a; }
return b;
}, '');
aholds the intermediate result,
bholds the current element.
a保存中间结果,
b保存当前元素。
回答by DerMike
If you would like to eliminate all the undefined, null, NaN, "", 0, a simple way to do it is to use a combination of filter call back function and boolean function.
如果你想消除所有的 undefined、null、NaN、""、0,一个简单的方法是使用过滤回调函数和布尔函数的组合。
var filterArr=arr.filter(function(val){
return Boolean(val);
});
When you pass a value to the boolean function, if the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false.
将值传递给布尔函数时,如果该值被省略或为 0、-0、null、false、NaN、undefined 或空字符串 (""),则该对象的初始值为 false。
Here is an example of this usage:
下面是这种用法的一个例子:
function bouncer(arr) {
var filterArr=arr.filter(function(val){
return Boolean(val);
});
return filterArr;
}
Here are some tests:
以下是一些测试:
bouncer([1, null, NaN, 2, undefined]);//should return [1, 2]
bouncer([7, "ate", "", false, 9]);// should return [7, "ate", 9]
bouncer(["a", "b", "c"]);// should return ["a", "b", "c"]
bouncer([false, null, 0, NaN, undefined, ""]);//should return []
回答by IamMHussain
Use the following code to remove the nullvalues only, its short & simple:
使用以下代码null仅删除值,其简短而简单:
addresses = addresses.filter(n => (n===null) ? false : true).join(', ');
document.client.cli_PostalAddress.value = addresses;
If you want to remove null, 0, false& ""(Empty String) like values, then use this:
如果要删除null, 0, false& ""(Empty String) 之类的值,请使用以下命令:
document.client.cli_PostalAddress.value = addresses.filter(Boolean).join(', ');
回答by rehan
addresses.filter(Boolean).join(", ")
address.filter(Boolean).join(", ")

