同时保持记录不从循环数组中取出空字符串? [英] Remove empty strings from array while keeping record Without Loop?

查看:116
本文介绍了同时保持记录不从循环数组中取出空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题在这里问:
<一href=\"http://stackoverflow.com/questions/18113243/remove-empty-strings-from-array-while-keeping-record-of-indexes-with-non-empty-s\">Remove从阵列空字符串,同时保持与非空字符串的索引记录

如果你注意到给出@Baz奠定出来;

 我,AM,,仍然,这里,,人

,并从这个我想产生以下两个数组:

 我,AM,仍然,这里,人

所有的答案的这个问题提到循环的一种形式。

我的问题:是否有可能为删除所有首页 ES与 字符串 没有任何循环? ...有没有其他的替代除了遍历数组?

可能有些正则表达式或者一些的jQuery ,我们不知道的?

所有的答案和建议是非常AP preciated。


解决方案

  VAR ARR = [我,AM,,仍然,这里, , 人]
// ARR = [我,AM,,仍然,这里,,人]
ARR = arr.filter(布尔)
// ARR = [I,上午,仍然,这里,人]

<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter\"><$c$c>filter文档


  // ARR = [我,AM,,仍然,这里,,人]
ARR = arr.filter(V =&GT;!V ='');
// ARR = [I,上午,仍然,这里,人]

<一个href=\"https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions\">Arrow功能文档

This question was asked here: Remove empty strings from array while keeping record of indexes with non empty strings

If you'd notice the given as @Baz layed it out;

"I", "am", "", "still", "here", "", "man"

"and from this I wish to produce the following two arrays:"

"I", "am", "still", "here", "man"

All the Answers to this question referred to a form of looping.

My question: Is there a possibility to remove all indexes with empty string without any looping? ... is there any other alternative apart from iterating the array?

May be some regex or some jQuery that we are not aware of?

All answers or suggestions are highly appreciated.

解决方案

var arr = ["I", "am", "", "still", "here", "", "man"]
// arr = ["I", "am", "", "still", "here", "", "man"]
arr = arr.filter(Boolean)
// arr = ["I", "am", "still", "here", "man"]

filter documentation


// arr = ["I", "am", "", "still", "here", "", "man"]
arr = arr.filter(v=>v!='');
// arr = ["I", "am", "still", "here", "man"]

Arrow functions documentation

这篇关于同时保持记录不从循环数组中取出空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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