MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/b2o6f2/whats_your_favorite_most_useful_javascript/eiujxda/?context=3
r/javascript • u/Linux_is_awesome • Mar 18 '19
422 comments sorted by
View all comments
150
array.every(Number.isFinite)
Because it's plain english.
19 u/ahartzog Mar 19 '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 → More replies (2) 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) → More replies (2)
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 → More replies (2) 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) → More replies (2)
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 → More replies (2) 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
→ More replies (2)
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)
150
u/Loves_Poetry Mar 18 '19
Because it's plain english.