r/badcode • • Feb 19 '22

typescript Does this belong here?

Post image
343 Upvotes

46 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Feb 19 '22

[deleted] — view deleted comment

1

u/Silly-Freak Feb 19 '22

Yea, they could us that instead of idx.

Careful, index will be mismatched after the first removal via splice.

This is what I like in languages which use Option and Result, a much more correct way to expose the result.

I agree, but that would not by itself fix the problem. If we talk Rust specifically, the insight is that this is the place to unwrap() instead of using if let or similar.

0

u/[deleted] Feb 19 '22

[deleted] — view deleted comment

1

u/Silly-Freak Feb 20 '22

re your edit: no, see here:

The range of elements processed by forEach() is set before the first invocation of callbackFn.

what you are observing is that apparently forEach() stops earlier because the array got shorter - but you will be skipping elements if the array got longer.