从Javascript中的数组中删除空元素 [英] Remove empty elements from an array in Javascript

查看:35
本文介绍了从Javascript中的数组中删除空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JavaScript 中从数组中删除空元素?

How do I remove empty elements from an array in JavaScript?

有没有直接的方法,还是我需要遍历它并手动删除它们?

Is there a straightforward way, or do I need to loop through it and remove them manually?

推荐答案

这个问题大约在九年前得到了回答,当时 Array 中没有很多有用的内置方法.prototype.

This question was answered almost nine years ago when there were not many useful built-in methods in the Array.prototype.

现在,当然,我建议您使用 filter 方法.

Now, certainly, I would recommend you to use the filter method.

请记住,此方法将返回一个新数组,其中包含传递您提供给它的回调函数条件的元素.

Take in mind that this method will return you a new array with the elements that pass the criteria of the callback function you provide to it.

例如,如果要删除 nullundefined 值:

For example, if you want to remove null or undefined values:

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];

var filtered = array.filter(function (el) {
  return el != null;
});

console.log(filtered);

这取决于您认为空"的内容,例如,如果您正在处理字符串,则上述函数不会删除空字符串的元素.

It will depend on what you consider to be "empty" for example, if you were dealing with strings, the above function wouldn't remove elements that are an empty string.

我经常看到的一个典型模式是删除falsy的元素,其中包括一个空字符串""0NaNnullundefinedfalse.

One typical pattern that I see often used is to remove elements that are falsy, which include an empty string "", 0, NaN, null, undefined, and false.

可以传递给filter方法,Boolean构造函数,或者在过滤条件函数中返回相同的元素,例如:

You can pass to the filter method, the Boolean constructor function, or return the same element in the filter criteria function, for example:

var filtered = array.filter(Boolean);

var filtered = array.filter(function(el) { return el; });

在两种情况下,这都有效,因为在第一种情况下,filter 方法将 Boolean 构造函数作为函数调用,转换值,而在第二种情况下,filter 方法在内部将回调的返回值隐式转换为 Boolean.

In both ways, this works because the filter method in the first case, calls the Boolean constructor as a function, converting the value, and in the second case, the filter method internally turns the return value of the callback implicitly to Boolean.

如果您正在处理稀疏数组,并且您正试图摆脱漏洞",您可以使用 filter 方法传递一个返回 true 的回调,例如:

If you are working with sparse arrays, and you are trying to get rid of the "holes", you can use the filter method passing a callback that returns true, for example:

var sparseArray = [0, , , 1, , , , , 2, , , , 3],
    cleanArray = sparseArray.filter(function () { return true });

console.log(cleanArray); // [ 0, 1, 2, 3 ]

旧答案:不要这样做!

我使用这个方法,扩展了原生的Array原型:

I use this method, extending the native Array prototype:

Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

test = new Array("", "One", "Two", "", "Three", "", "Four").clean("");
test2 = [1, 2,, 3,, 3,,,,,, 4,, 4,, 5,, 6,,,,];
test2.clean(undefined);

或者您可以简单地将现有元素推入其他数组:

Or you can simply push the existing elements into other array:

// Will remove all falsy values: undefined, null, 0, false, NaN and "" (empty string)
function cleanArray(actual) {
  var newArray = new Array();
  for (var i = 0; i < actual.length; i++) {
    if (actual[i]) {
      newArray.push(actual[i]);
    }
  }
  return newArray;
}

cleanArray([1, 2,, 3,, 3,,,,,, 4,, 4,, 5,, 6,,,,]);

这篇关于从Javascript中的数组中删除空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆