Can forEach in JavaScript make a return?

I wonder if forEach in JavaScript can return, here is my code:

var a = [0, 1, 2, 3, 4]; function fn(array) { array.forEach(function(item) { if (item === 2) return false; }); return true; } var ans = fn(a); console.log(ans); // true 

I want to find out if 2 is in my array, if so, return false, but it seems that the forEach function has looped the entire array and ignored return.

I wonder if I can get the answer I want for forEach (I know that I can get what I want to use for (..))? Dear friend, help me, with great thanks!

+5
source share
5 answers

You can use indexOf instead of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

 function fn(array) { return (array.indexOf(2) === -1); } 

Also from the documentation for forEach - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Note. Cannot stop or break a forEach () loop other than throwing an exception. If you need this behavior, the .forEach () method is the wrong tool, use a simple loop instead.

Therefore, the return value cannot be used the way you use it. However, you can make a roll (which is not recommended unless you really need a mistake that needs to be raised there)

 function fn(array) { try { array.forEach(function(item) { if (item === 2) throw "2 found"; }); } catch (e) { return false; } return true; } 
+6
source

In this case .indexOf() is probably what you want, but there is also .some() when a simple comparison of comparisons is too simple:

 var ans = a.some(function(value) { return value === 2; }); 

The .some() function returns true if the callback returns true for any element. (The function returns as soon as the callback returns true , so it does not bother to look at the first match.)

You can usually use the .reduce() function as a more general iteration mechanism. If you want to count how many instances of 2 were in your array, for example:

  var twos = a.reduce(function(c, v) { if (v === 2) c += 1; return c; }, 0); 
+4
source

forEach returns undefined by specification . If you want to know if there is a specific value in an array, there is indexOf. For more complex problems, there is some function that allows the function to check values ​​and returns true the first time the function returns true or false otherwise:

 a.some(function(value){return value == 2}) 

Obviously, this is a trivial case, but consider whether you want to determine if the array contains any even numbers:

 a.some(function(value){return !(value % 2)}) 

or as a function of the arrow ECMA2015:

 a.some(value => !(value % 2)); 

If you want to check if a specific value is repeated in an array, you can use lastIndexOf :

 if (a.indexOf(value) != a.lastIndexOf(value) { // value is repeated } 

or to check for any duplicates, some of them will do the following:

 var hasDupes = a.some(function(value, i, a) { return a.lastIndexOf(value) != i; }); 

The advantage of some and each is that they process only the elements of the array until the condition is satisfied, and then they exit, while forEach will process all the members independently.

+1
source

Others mentioned .indexOf() and .some() . I thought I would add that the good old-fashioned for loop gives you the vast majority of iterations because you control the iteration and your processing code is not built into the callback function.

Although .indexOf() already doing exactly what you need, this code just shows how you can directly return when using the old-fashioned for loop. These days it’s somehow not to your liking, but often remains the best choice for better cycle management.

 function fn(array) { for (var i = 0, len = array.length; i < len; i++) { if (array[i] === 2) return false; } return true; } 

Using the for loop, you can iterate backward (useful when deleting elements from an array during an iteration), you can insert elements and adjust the iteration index, you can return immediately, you can skip indexes, etc.

0
source

You said you want with forEach, so I changed your code:

 function fn(array) { var b = true; array.forEach(function (item) { if (item === 2) { b = false; } }); return b; } 
0
source

All Articles