gpt4 book ai didi

javascript - 从数组中删除空字符串,同时保留无循环记录?

转载 作者:IT王子 更新时间:2023-10-29 02:45:44 25 4
gpt4 key购买 nike

这个问题是在这里问的: Remove empty strings from array while keeping record of indexes with non empty strings

如果您注意到@Baz 给出的布局;

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

“我希望由此生成以下两个数组:”

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

这个问题的所有答案都涉及一种循环形式。

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

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

非常感谢所有的回答或建议。

最佳答案

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

关于javascript - 从数组中删除空字符串,同时保留无循环记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19888689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com