本文介绍了去除字符串数组一个空字符串 - JQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组 [排版,,文字]
。我想从这个数组中删除空字符串,并且获得 [排版,文字]
。
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?
推荐答案
您可以使用的:
You may use filter
:
var newArray = oldArray.filter(function(v){return v!==''});
有一种变通方法IE8的兼容性。您还可以使用一个很好的老循环,如果你不打算使用过滤
其他地方,有一个与循环没有问题...
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的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!