admin管理员组文章数量:1415145
My iteration has been working fine and dandy until know. I've encountered myself with an empty array and the .every() method doesn't work with that kind of values.
Here's the validation:
if(oJSonElementByIndex[sColumnName].every(x => typeof x == 'number'))
¿Any other options? Thanks in advance.
My iteration has been working fine and dandy until know. I've encountered myself with an empty array and the .every() method doesn't work with that kind of values.
Here's the validation:
if(oJSonElementByIndex[sColumnName].every(x => typeof x == 'number'))
¿Any other options? Thanks in advance.
Share Improve this question asked Jun 14, 2018 at 14:00 Jose PeresJose Peres 3171 gold badge2 silver badges19 bronze badges 6- 3 What do you mean it doesn't work? What exactly do you want to achieve? – bugs Commented Jun 14, 2018 at 14:01
- I think the one line of code is quite self-explaining in terms of what he wants to achieve... – Rob Commented Jun 14, 2018 at 14:02
- @Robert not really... the question as a whole requires clarification – bugs Commented Jun 14, 2018 at 14:03
-
3
Every value of an empty array verifies any predicate you want. If that's not to your taste, you might want to test whether the array is empty beforehand, i.e.
if(array.length > 0 && array.every(predicate))
– Aaron Commented Jun 14, 2018 at 14:03 - "I've encountered myself with an empty array and the .every() method doesn't work with that kind of values". If you have an object like this one: "key": [], the every() method won't suffice. – Jose Peres Commented Jun 14, 2018 at 14:04
2 Answers
Reset to default 7If there is no element in the array, every element in the array fullfills the condition. Therefore it returns true. To achieve the opposite:
arr.length && arr.every(/*...*/)
So the ment about "every" predicate being fulfilled on an empty array, and the ment about using array.length
and not just the array led me to putting together this snippet to illustrate.
The things to note here are the truthiness of an empty array, the falsiness of an empty array's length--zero, and the somewhat unintuitive logic idea that if you have no predicates then all none of them passed the condition:
const empty = []
const typeofNumber = (x) => ( typeof x === 'number' )
console.log(`an empty array will return ${empty.every(typeofNumber)} on an every, since "every" predicate fulfilled the condition`)
if(empty.length && empty.every(typeofNumber)) {
console.log('empty array length and every are truthy')
} else {
console.log('empty array length and every are falsey')
}
if(empty && empty.every(typeofNumber)) {
console.log('empty array and every are truthy')
} else {
console.log('empty array and every are falsey')
}
本文标签: javascriptIs there a method like every() for arrays with empty valuesStack Overflow
版权声明:本文标题:javascript - Is there a method like .every() for arrays with empty values? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745212365a2647965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论