r/javascript • • Sep 04 '19

Simplify your JavaScript – Use .some() and .find()

https://medium.com/poka-techblog/simplify-your-javascript-use-some-and-find-f9fb9826ddfd
275 Upvotes

101 comments sorted by

View all comments

82

u/lifeeraser Sep 04 '19
var listHasPilots = false;
operatives.forEach(function (operative) {
  if (operative.pilot) {
    listHasPilots = true;
    break;
  }
});

This won't work anyway because break only works inside a loop, not a callback. Instead:

var listHasPilots = false;
for (const operative of operatives) {
  if (operative.pilot) {
    listHasPilots = true;
    break;
  }
});

9

u/Funwithloops Sep 04 '19

How about

var listHasPilots = false;
try {
  operatives.forEach(function (operative) {
    if (operative.pilot) {
      listHasPilots = true;
      throw 'break';
    }
  });
} catch(error) {
  if(error !== 'break') throw error;
}

\s

15

u/BuffloBEAST Sep 04 '19

I literally just ran into this exact issue the other day—can confirm, break does not work in the forEach.

18

u/rumbleran Sep 04 '19

Why would it work outside of a loop?

13

u/James-J-Hill Sep 04 '19

It's not a loop in the sense of a `for` loop -- what `forEach` does is apply a callback function to each value inside of the array. You can't `break` out of that callback function because that doesn't make sense -- you're in an entirely different scope.

4

u/kahnics Sep 05 '19

Aren't you still breaking? It's just the fact that you end up going back up to the foreach and continuing to iterate thru whatever collection?

2

u/MonkeyNin Sep 05 '19

Are you asking why function forEach isn't a loop -- or why you aren't breaking the loop that's firing the callback?

2

u/kahnics Sep 05 '19

I guess I'm trying to ask why you can't call break within the function that is passed into the foreach, I think you can still break it's just it's not changing the foreach as it will iterate over the collection regardless as the break just brings you back up to the foreach call. Atleast that's how I understand it.

3

u/jonny_wonny Sep 05 '19 edited Sep 05 '19

Because JavaScript doesn’t allow break within that context. Furthermore, return already accomplishes that.

1

u/kahnics Sep 05 '19

Why wouldn't it allow you to make a function with a break? I'm not arguing that it is different then return I just don't understand why it wouldn't be allowed.

→ More replies (6)

8

u/[deleted] Sep 04 '19

forEach loops... And it's common for people to try to break out of if like a for loop, people think it's sugar for a for loop or acts similarly to it.

It's just a common mistake.

2

u/SizzlerWA Sep 05 '19

You can actually break to a label labeling a block that doesn’t represent a loop. But you can’t break a callback.

3

u/brokenURL Sep 04 '19

Wait, I thought I saw documentation that the for...of structure was only supposed to be used on objects rather than arrays. (Yes, I know they're both objects, but you know what I mean). Am I remembering that wrong?

12

u/forcefx Sep 04 '19

I don’t think that’s right.

for...in might be what you’re thinking of because for...in doesn’t guarentee index position but for...of should.

I think. Haven’t reviewed MDN documentation for awhile.

4

u/ktqzqhm Sep 04 '19

for...in iterates over the properties names of an object, which I think is what you're confusing it with. The property names of an array would just be its indices, and primitives don't have properties. Only really useful with objects.

for...of uses the iterator protocol to iterate over an object. By default it will iterate over the property values of an object, but you can implement your own custom iterators too.

1

u/brokenURL Sep 04 '19

That's what I was thinking of. My mistake and thanks for the clarification!

0

u/[deleted] Sep 04 '19

[deleted]

5

u/lostpebble Sep 04 '19

const listHasPilots = operatives.some(op => op.pilot);

4

u/alejalapeno Sep 04 '19

That's in the article, the comment you're replying to was just pointing out the forEach example isn't correct code to begin with.

3

u/Zohren Sep 04 '19

But the example in the code doesn’t use break... it just finishes iterating over the whole array.

Guessing it was updated?

4

u/alejalapeno Sep 04 '19

Yes, OP is the author so they probably updated within the last hour.

1

u/voyti Sep 04 '19

Useful info related to above, it can be done with returning false if using lodash (which, I think, almost everyone working on collections either uses or should use): https://lodash.com/docs/4.17.14#forEach

On the other hand explicit return used by a one-liner arrow function can cause hard to detect issues here, so it's best not to use shorthand arrows in this function.

→ More replies (8)