从 JavaScript 数组中删除空值

发布于 2024-08-19 06:01:14 字数 448 浏览 5 评论 0原文

我有一个 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 am having a javascript array.

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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

策马西风 2024-08-26 06:01:14

您可以使用 filter 进行过滤输出 null 值:

addresses.filter(function(val) { return val !== null; }).join(", ")

You can use filter to filter out the null values:

addresses.filter(function(val) { return val !== null; }).join(", ")
梦亿 2024-08-26 06:01:14

使用过滤器 删除所有 falsy 值的方法:

var list = [null, undefined, 0, 1, 2, '', 'test'];

// ES5:
var result = list.filter(Boolean).join(', ');
console.log(result);

// ES6, saves 3 characters:
var result = list.filter(x => x).join(', ');
console.log(result);

Use filter method to remove all falsy values:

var list = [null, undefined, 0, 1, 2, '', 'test'];

// ES5:
var result = list.filter(Boolean).join(', ');
console.log(result);

// ES6, saves 3 characters:
var result = list.filter(x => x).join(', ');
console.log(result);

俯瞰星空 2024-08-26 06:01:14

另一种过滤器替代品

myArray.filter(function(val){if(val)return val}).join(", ")

document.write(['this','','',,,,'is','a',,,'test'].filter(function(val){if(val)return val}).join(", "));

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(", "));

棒棒糖 2024-08-26 06:01:14

Underscore 是一个很好的实用程序库,用于函数式编程和列表操作:

_.filter(addresses, function(val) { return val !== null; }).join(", ");

编辑:还有一个更紧凑的方式(感谢安德鲁·德·安德拉德!):

_.compact(addresses).join(", ");

Underscore is a nice utility library for functional programming and list manipulation:

_.filter(addresses, function(val) { return val !== null; }).join(", ");

Edit: And there is a more compact way (Thanks Andrew De Andrade!):

_.compact(addresses).join(", ");
感情旳空白 2024-08-26 06:01:14

地址.filter(Boolean).join(", ")

addresses.filter(Boolean).join(", ")

迷你仙 2024-08-26 06:01:14
document.client.cli_PostalAddress.value = 
    document.client.cli_PostalAddress.value.replace(/(, )+/g,", ");

或者应该是

document.client.cli_PostalAddress.value = 
    document.client.cli_PostalAddress.value
    .replace(/(null, )/g,"").replace(/(, null)/g,"");

??

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,"");

??

最初的梦 2024-08-26 06:01:14

还可以使用 Array.prototype.reduce()

它将数组减少为单个值,例如字符串。像这样:

addresses.reduce(function (a, b) {
        if (a && b) { return a + ', ' + b; }
        if (a) { return a; }
        return b;
      }, '');

a 保存中间结果,
b 保存当前元素。

One could also use Array.prototype.reduce().

It reduces an 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;
      }, '');

a holds the intermediate result,
b holds the current element.

小矜持 2024-08-26 06:01:14

如果你想消除所有的 undefined、null、NaN、""、0,一个简单的方法是使用过滤器回调函数和布尔函数的组合。

var filterArr=arr.filter(function(val){
   return Boolean(val);  
  });

当您将值传递给布尔函数时,如果该值被省略或者为 0、-0、null、false、NaN、undefined 或空字符串 (""),则该对象的初始值为 false。

以下是此用法的示例:

 function bouncer(arr) {

         var filterArr=arr.filter(function(val){
           return Boolean(val);  
          });

      return filterArr;
    }

以下是一些测试:

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

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.

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.

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 []
动次打次papapa 2024-08-26 06:01:14

使用以下代码仅删除 null 值,其简短的 & simple:

addresses = addresses.filter(n => (n===null) ? false : true).join(', ');
document.client.cli_PostalAddress.value = addresses;

如果你想删除 null, 0, false & ""(空字符串)之类的值,然后使用:

document.client.cli_PostalAddress.value = addresses.filter(Boolean).join(', ');

Use the following code to remove the null values only, its short & simple:

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:

document.client.cli_PostalAddress.value = addresses.filter(Boolean).join(', ');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文