简体   繁体   English

如何使用javascript foreach循环获取返回四?

[英]How to use javascript foreach loops to get return four?

There is an array and I want to use foreach loop to get the biggest number 有一个数组,我想使用foreach循环获取最大数量

Here is an array 这是一个数组

 const array2 = ['a', 3, 4, 2]

What i try in JS: 我在JS中尝试的是:

 function biggestNumberInArray3(arr) {
  var largest = arr[0] || null;
  var number = null;
  arr.forEach (value => {
    number = value
    largest = Math.max(largest, number);
   })
  return largest;
  }

Looks like Math.max isn't work in here. 看起来Math.max在这里不起作用。

It returns NaN 返回NaN

Are there any other ways to use foreach loop to compare the elements in an array? 还有其他方法可以使用foreach循环比较数组中的元素吗?

PS: this foreach loop will return 4 PS:此foreach循环将返回4

You should use Array.reduce to find the max number and filter it before the max operation as the presence of a will cause the result to be a NaN . 您应该使用Array.reduce找到最大数量和filter的最大操作的存在之前, a将导致的结果是一个NaN

 const array2 = ['a', 3, 4, 2] var max = array2.filter((num)=> !isNaN(num)).reduce((a, b)=>{ return Math.max(a, b); }); console.log(max); 

forEach don't return any value. forEach不返回任何值。

You can use Filter and Math.max 您可以使用FilterMath.max

  1. use filter to remove all non-number values. 使用过滤器删除所有非数字值。
  2. use Math.max to get highest value. 使用Math.max获得最高价值。

  const array2 = ['a', 3, 4, 2] console.log(Math.max(...array2.filter(e=> !isNaN(e)))) 

const array2 = ['a', 3, 4, 2]
var max = Math.max(...array2.filter(num => Number.isInteger(num)));
console.log(max);

Remove the string from array, see demo. 从数组中删除字符串,请参见演示。 Not interested in efficient and simple code? 对高效简单的代码不感兴趣? forEach() only returns undefined so you'll need to get a side-effect in order to get any result. forEach()仅返回undefined因此您需要获得副作用才能获得任何结果。 In the demo below there's a variable outside the loop that changes as the loop progresses. 在下面的演示中,循环外有一个变量,随着循环的进行而变化。 Eventually this variable will be the greatest number in the array. 最终,此变量将是数组中的最大数字。


Demo 演示版

 /* Math.max.apply() ======================================*/ const array = ['a', 3, 4, 2]; //Get rid of the string const num = array.shift(); console.log(`Math.max.apply({}, arr) ========= ${Math.max.apply({}, array)}`); /* forEach() =======================================*/ let max = 0; array.forEach(num => { max = num > max ? max = num : max; }); console.log(`array.forEach() ================ ${max}`); 

If you want to use forEach , you can just add a check for numbers in your code 如果要使用forEach,只需在代码中添加数字检查即可

  function biggestNumberInArray3(arr) { var largest = null; var number = null; arr.forEach (value => { if(typeof(value) === "number"){ number = value largest = Math.max(largest, number); } }) return largest; } console.log(biggestNumberInArray3(['a', 3, 4, 2])) 

Here you have another solution using only reduce() : 在这里,您还有一个只使用reduce()解决方案:

 const array2 = ['a', 3, 4, 2, "hello", {hello:"world"}]; let res = array2.reduce((max, e) => isNaN(e) ? max : Math.max(max, e), null); console.log(res); 

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

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