我正在检查这样的元素数组中是否有任何“”值
"" in line.map(function(){return $(this).val();}).get()
它返回 false,但是当我在浏览器上调试它时,我在数组上得到了这个
line.map(function(){return $(this).val();}).get() = ["1", "2", "4", "", "", "", "", "", "3"]
我想这应该返回true,对吧?
我已经尝试过 .each
但我无法返回它,除非我使用带有 return false;
最佳答案
The in
operator不检查数组是否包含值(即 indexOf
或 includes
),它枚举对象的属性。例如:
const data = ['a', 'b', 'c', 'd', 'e', 'f'];
for (f in data) {
console.log('gets the index:', f);
console.log('gets the value:', data[f]); // <- note the access using f
}
相反,您应该使用 find
如果您需要元素或 some
如果您只需要检查是否存在:
const data = ['a', 'b', 'c', 'd', 'e', 'f'];
console.log('find an element by value:', data.find(it => it === 'c'));
console.log('check if any elements match:', data.some(it => it === 'c'));
关于javascript - ""in line.map(function(){return $(this).val();}).get(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40137308/