从字符串数组中删除一个空字符串 - JQuery

问题描述:

我有一个数组["Lorem", "", "ipsum"].我想从这个数组中删除空字符串并得到 ["Lorem", "ipsum"].

I have an array ["Lorem", "", "ipsum"]. I would like to remove the empty string from this array and get ["Lorem", "ipsum"].

有没有办法在不使用循环并遍历每个元素并删除它的情况下做到这一点?

Is there any way to do this without using the loop and traversing through each element and removing it?

您可以使用 filter :

You may use filter :

var newArray = oldArray.filter(function(v){return v!==''});

MDN 有一个解决方法IE8 兼容性.如果您不打算在其他任何地方使用 filter,您也可以使用一个很好的旧循环,循环没有问题...

The MDN has a workaround for IE8 compatibility. You might also use a good old loop if you're not going to use filter anywhere else, there's no problem with looping...