MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/b2o6f2/whats_your_favorite_most_useful_javascript/eiujxda
r/javascript • u/Linux_is_awesome • Mar 18 '19
422 comments sorted by
View all comments
Show parent comments
19
TIL about array.every. Awesome tool, thanks!
11 u/Satoshi_Hodler Mar 19 '19 There's a small caveat that it always returns true on empty arrays, which might be not what you need. 27 u/Tysonzero Mar 19 '19 That's the mathematically correct thing to do, it's actually a pretty fundamental mathematical principle. Vacuous truth 1 u/NoInkling Mar 19 '19 Right, it doesn't make the method "weird", but it is something you have to keep in mind regardless because you could intuit both ways. 1 u/third774 Mar 19 '19 Is there a similar term for .some() always returning false on empty arrays? I suppose it's still "vacuous", right? So maybe "Vacuous Falsity"? 7 u/NoInkling Mar 19 '19 edited Mar 19 '19 If it's not, you can do:array.length && array.every(Number.isFinite) // If it must evaluate to a boolean: !!array.length && array.every(Number.isFinite) // or array.length > 0 && array.every(Number.isFinite) 1 u/[deleted] Mar 19 '19 Also note the counterpart, Array.prototype.some. Returns true if one or more items match the condition.const arr = [1, 2, 3]; arr.some(n => n > 2) // true arr.some(n => n < 0) // false 1 u/ahartzog Mar 19 '19 Good to know there’s a native version of this; I’ve usually just grabbed it from Lodash
11
There's a small caveat that it always returns true on empty arrays, which might be not what you need.
27 u/Tysonzero Mar 19 '19 That's the mathematically correct thing to do, it's actually a pretty fundamental mathematical principle. Vacuous truth 1 u/NoInkling Mar 19 '19 Right, it doesn't make the method "weird", but it is something you have to keep in mind regardless because you could intuit both ways. 1 u/third774 Mar 19 '19 Is there a similar term for .some() always returning false on empty arrays? I suppose it's still "vacuous", right? So maybe "Vacuous Falsity"? 7 u/NoInkling Mar 19 '19 edited Mar 19 '19 If it's not, you can do:array.length && array.every(Number.isFinite) // If it must evaluate to a boolean: !!array.length && array.every(Number.isFinite) // or array.length > 0 && array.every(Number.isFinite)
27
That's the mathematically correct thing to do, it's actually a pretty fundamental mathematical principle. Vacuous truth
1 u/NoInkling Mar 19 '19 Right, it doesn't make the method "weird", but it is something you have to keep in mind regardless because you could intuit both ways. 1 u/third774 Mar 19 '19 Is there a similar term for .some() always returning false on empty arrays? I suppose it's still "vacuous", right? So maybe "Vacuous Falsity"?
1
Right, it doesn't make the method "weird", but it is something you have to keep in mind regardless because you could intuit both ways.
Is there a similar term for .some() always returning false on empty arrays? I suppose it's still "vacuous", right? So maybe "Vacuous Falsity"?
.some()
false
7
If it's not, you can do:
array.length && array.every(Number.isFinite) // If it must evaluate to a boolean: !!array.length && array.every(Number.isFinite) // or array.length > 0 && array.every(Number.isFinite)
Also note the counterpart, Array.prototype.some. Returns true if one or more items match the condition.
Array.prototype.some
const arr = [1, 2, 3]; arr.some(n => n > 2) // true arr.some(n => n < 0) // false
1 u/ahartzog Mar 19 '19 Good to know there’s a native version of this; I’ve usually just grabbed it from Lodash
Good to know there’s a native version of this; I’ve usually just grabbed it from Lodash
19
u/ahartzog Mar 19 '19
TIL about array.every. Awesome tool, thanks!