massiv.forEach(function(item, i) { if (item == "1") { console.log(i); return false; } }); 

There is an array with values.

Inside the loop there is a condition for finding the value of the array.

Task: stop further iteration of the cycle at the first value found.

Thanks for the help.

Solution: (thanks to Lieutenant Jim Dangle)

 [1, 2, 3, 4, 3].some(function(el,i) { if (el == 3) { console.log(i); return el; } }); 
  • one
    @LieutenantJimDangle, your code doesn't work the same as the code in question :-) - Grundy
  • @LieutenantJimDangle, it just removes the syntax error - Grundy
  • Add to the question an example of your array, and what exactly in it you want to find and display. - Grundy
  • Solution code is invalid. the result returned by the Callback function passed to some needed only to check whether to stop passing through the collection or not. Therefore, if suddenly the desired element will be 0 - then in your case some will pass through the entire collection - Grundy

4 answers 4

The forEach method does not provide for the ability to abort collection traversal.

If you need to check for the presence of a specific element satisfying the condition, it is better to use some

 var massiv = ['2', '1', '3']; console.log(massiv.some(function(item) { console.log('some', item); return (item == "1"); })); 

If objects are checked completely, you can use the indexOf method

 var massiv = ['2', '1', '3']; console.log(massiv.indexOf("1")); 

If you need to find the index of an element that satisfies the condition, then the findIndex method is suitable.

 var massiv = ['2', '1', '3']; console.log(massiv.findIndex(function(item) { console.log('findIndex', item); return item == "1"; })); 


If you really want to use forEach , you can put it in try..catch and throw an error if the item is found.

 var massiv = ['2', '1', '3']; try { massiv.forEach(function(item, i) { console.log('foreach', i); if (item == "1") { throw { reason: "finded", index: i } } }); } catch ({ reason, index }) { if (reason) { console.log(reason, ':', index) } } 


Instead of the built-in array functions, you can also use the for loop. In this case, to break the loop, you can use the expression break

  • In all cases, you have exactly the search for a specific item. And the task was to find the first one found. In your interpretation of the array, it would be like this: var massiv = ['2', '1', '3', '1']; And you need to find the index of the first unit found (that is, the result of the stop would be the value 1 (the second element of the array) - Typography Manager
  • findIndex Manager, all methods some , findIndex - stop execution as soon as the callback returns true . The indexOf method, finds the first occurrence of an element. starting from the specified index, since the index is not specified, the check starts from the first element of the array. - Grundy
  • @Typography Manager, if the task is simply to check the presence of elements in the array. then foreach is not needed at all. Otherwise, all the methods do the same as the code in question, except for stopping the execution when the condition is met - Grundy

In the forEach method, there is no stop as in a loop through a break.

Use every forEach instead. When it is false , everything will stop.

This is the most correct way.

 massiv.every(function(item, i) { if (item == "1") { console.log(i); return false; } else { return true; } }); 
  • account This is the most correct way - one can argue - Grundy
  • I gave an example of exactly the question posed, it does what is necessary @ Typography manager, all other answers, in my opinion, are not effective. there is a clearly posed question, and there is a solution ... - L. Vadim
  • one
    some does exactly the same thing. the difference is only in the sign of comparison. And all this is less effective than normal for due to the constant call of the callback function. In addition, it is not known what the author needs at all, what exactly he wants to do with the received element. does he need an element or element index, etc. - Grundy
  • here you can argue long - L. Vadim
  • What I wrote in my first comment :) - Grundy

 [1, 2, 3].some(function(el) { console.log(el); return el === 2; }); 

  • All the buzz, just slightly refined the code (in the first message) - Typography Manager

You can stop the loop by calling a non-existent function and catching the error:

 var massiv = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] try { massiv.forEach(function(item, i, vvv) { console.log(item); if (item == "5") { vvv(); } }); } catch (e) {} 

  • worth adding an explanation of what is happening - Grundy
  • one
    Instead of calling a non-existing function, it would be worth using a throw statement. - Pavel Mayorov