gpt4 book ai didi

Javascript 检查数组中至少 2 个字符串非空

转载 作者:行者123 更新时间:2023-11-29 16:34:40 25 4
gpt4 key购买 nike

我有一个由 4 个字符串组成的数组,名为 options

我目前正在检查其中的所有元素是否非空:

options.every(option => Boolean(option))

我想检查至少 2 个非空,一些示例:

有效

["opt1", "opt2", "", ""]  
["opt1", "opt2", "", "opt4"]

无效

["opt1", "", "", ""]  
["", "", "", ""]

感谢任何帮助!

最佳答案

function hasEmptyStrings(arr, n) {
return arr.filter(e => !e).length <= n
}

var arrays = [
["opt1", "opt2", "", ""],
["opt1", "opt2", "", "opt4"],
["opt1", "", "", ""],
["", "", "", ""]
]

arrays.forEach(arr => {
console.log(hasEmptyStrings(arr, 2))
})

关于Javascript 检查数组中至少 2 个字符串非空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52623914/

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