Re:FP techniques that will help you write better JavaScript
by bryanrasmussen on Saturday February 12, 2022 @03:06AM
(#30310883)
yes, you showed this earlier https://news.ycombinator.com/item?id=30306256 by mutating the array parameter of the map, but I made my wrong statement before reading your example.
I guess it's a failure of imagination on my part not to have realized that I could modify the array length in that way, or really the failure of imagination was not seeing any good reason for sending the original array in as a parameter to map - when I saw that in documentation I thought what a weird idea!
Because JS’s map and filter provide the index and array as arguments, you can, in fact, achieve the behavior of break. If you need the original array after the iteration, you need to apply the map or filter to a copy, though, because the way to achieve the behavior of break involves modifying the array by deleting all the items after the current one, which stops the iteration.