Hacker News new | ask | show | jobs
by dragonwriter 1585 days ago
> You can easily achieve the output of using break using filter, but you cannot achieve the behavior because filter goes over every item in your array.

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.

1 comments

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!