简体   繁体   English

如果语句在foreach循环javascript中不起作用

[英]If statement not working inside foreach loop javascript

I am trying to return the first value which is greater than 25 from an array, without using other methods such as filter.我试图从数组中返回大于 25 的第一个值,而不使用其他方法,例如过滤器。 Assume the array is always sorted.假设数组总是排序的。

The function always returns undefined.该函数始终返回未定义。 Why is that happening?为什么会这样? Here is the code:这是代码:

 function checkArr(arr){ arr.forEach(i => { if (i>25) return i; }) } console.log(checkArr([10,20,34,45]))

The output should be 34.输出应该是 34。

When you use forEach , you execute another function for every item in the array. 使用forEach ,将对数组中的每个项目执行另一个函数。 That function can return a value just fine, but it'll get completely ignored. 该函数可以返回一个很好的值,但是它将被完全忽略。 If you want to return a value in the checkArr function, you have to do something like this: 如果要在checkArr函数中返回一个值,则必须执行以下操作:

function checkArr(arr) {
  for (let i of arr) {
    if (i>25) return i;
  }
}

You can use find function instead of forEach . 您可以使用find函数代替forEach And you must return a result of this function to get an answer instead of undefined . 而且,您必须返回此函数的结果以获得答案,而不是undefined

 function checkArr(arr) { return arr.find(i => i > 25); } console.log(checkArr([10,20,34,45])) 

forEach function returns nothing, it just makes some actions for each element in an array. forEach函数不返回任何内容,它仅对数组中的每个元素执行一些操作。

Your function is not returning anything. 您的函数未返回任何内容。 And returning inside a forEach won't have any effect, since forEach always returns undefined . 并且在forEach内部返回不会有任何效果,因为forEach始终返回undefined You can use a simple while loop to do this: 您可以使用简单的while循环来执行此操作:

 function checkArr(arr){ var i = 0; while (i<arr.length) { if (arr[i]>25) return arr[i]; i++; } return null; // if no entry is greater than 25 } console.log(checkArr([10,20,34,45])); 

The forEach() method executes a provided function once for each array element. forEach()方法为每个数组元素执行一次提供的函数。

From the above statement it is clear that you can not return from the middle of the execution of the loop. 从上面的语句中可以明显看出,您不能从循环执行的中间返回。

Use for...of loop instead to return as soon as the condition is true : 当条件为true使用for...of循环返回:

 function checkArr(arr){ for(var item of arr){ if (item > 25) return item; } } console.log(checkArr([10,20,34,45])) 

Seems like you're looking for find : 似乎您在寻找find

 function checkArr(arr) { return arr.find(i => i>25); } console.log(checkArr([10,20,34,45])); 

It's because you're passing a delegate function when calling .forEach . 这是因为调用.forEach时要传递一个委托函数。

The return of the delegate is getting lost and isn't applying to anything. 代表的返回正迷路,并且没有适用于任何东西。 To get your desired result, you'll want to exit the calling function checkArr . 为了获得所需的结果,您需要退出调用函数checkArr

This can be done using a simple for loop. 可以使用简单的for循环来完成。

 function checkArr(arr){ for (var i = 0; i < arr.length++; i++) { if (arr[i] > 25) return arr[i]; } } console.log(checkArr([10,20,34,45])) 

This approach also supports older browsers, unlike some, every and forEach 这种方法还支持较旧的浏览器,与某些浏览器不同

function checkArr(arr){
    return arr.filter(m => m > 25)
}
checkArr([10,20,34,45]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM