by yakshaving_jgt · 4y ago · 0 comments
If you want something that behaves like:
array.map(x => {
if (x > 2) { break; } // can't really use break here!
else { return x; }
});
you do this: [...array].map((x,i,a) => {
if (x > 2) { a.splice(i); }
else { return x; }
});
(if you don’t need the array later, you can just use “array” instead of [...array]; the latter is just used because the break effect is achieved by mutating the array to delete all the elements subsequent to the one you are working, terminating the iteration, and copying the array initially means you don’t stomp the original.)Let me know what it is you're trying to achieve — at a high level, not in terms of specific implementation details — and I can show you how this might be expressed without using anything as low-level as a `for` loop.
Of course if it were assumed they never need anything but the higher level methods: map, reduce, some, every etc. - then they would probably use some or every if they wanted to break.
I suppose filter could maybe be hacked to do it in some cases dependent on implementation, if the implementation determined that it was impossible to ever return true for any of the items remaining in an array that implementation might effectively break the array processing however every way I can think of doing that would be artificial, horrible, and probably full of side effects.
No it doesn’t.
If I only need to do X
And X cannot do Y
Then I do not need to do Y
-----------------------------
If I need to do Y
And X cannot do Y
Then I need to do something other than X
-------------------------
Give your answer of no it doesn't please be so kind as to outline the logical reasoning that you are envisioning?
on edit: added line between logical statements.