Is there a mechanism in JavaScript (without having to write my own) similar to filter
. Instead of returning all the filtered elements of a collection though, it only returns the first one. Of course I could do the following to get the first even number:
[7,5,3,2,1].filter(x => x % 2 == 0)[0]
But if there were 10 million more numbers in that list, there'd be a lot of unnecessary work. In a language like Haskell, the other 10 million numbers wouldn't be looked at due to lazy evaluation.
Is there a mechanism in JavaScript to do the above without evaluating any elements after the first result?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…