本文介绍了从字符串数组中删除一个空字符串 - 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...
这篇关于从字符串数组中删除一个空字符串 - JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!