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

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

问题描述

这里有人问过这个问题:从数组同时保留非空字符串的索引记录

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

如果您注意到@Baz 列出的内容;

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

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

我希望由此产生以下两个数组:"

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

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

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

我的问题:是否有可能使用 empty string 删除所有 indexes任何循环? ...除了迭代数组之外还有其他选择吗?

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?

可能是一些我们不知道的 regex 或一些 jQuery?

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 文档

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

箭头函数文档

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

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