简体   繁体   English

在 function 中返回多个值

[英]returning multiple values in function

I'm practicing using the map method and i'm stuck on this problem.我正在练习使用 map 方法,但我被困在这个问题上。 I'm trying to return the largest number out of each of these arrays but for my function to be complete i need to return the push method outside of my nested map.我试图从这些 arrays 中返回最大的数字,但是为了让我的 function 完整,我需要在嵌套的 map 之外返回推送方法。 I have no idea how to do that.我不知道该怎么做。 Any input would be extremely appreciated.任何输入将不胜感激。 Thanks in advance.提前致谢。

 const bigAssArr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [100, 1001, 857, 1]] const biggestFour = []; const largestOfFour = (arr) => { return arr.map((item, index) => { let tempMax = item[0]; return item.map((i, iTwo) => { let currentValue = i; if (currentValue > tempMax ) { tempMax = currentValue } }) biggestFour.push(tempMax); // *** This is where i'm having the problem*** }) }

In ES6, you can use Math.max(...arr) to get the max number of an array.在 ES6 中,您可以使用Math.max(...arr)来获取数组的最大数量。

The Math.max() function returns the largest of the zero or more numbers given as input parameters. Math.max() function 返回作为输入参数给出的零个或多个数字中的最大值。

For example:例如:

 var arr = [4, 5, 1, 3];
 console.log(Math.max(...arr )); // Output: 5

After that, you can use .map to create a new array.之后,您可以使用.map创建一个新数组。

 const bigAssArr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [100, 1001, 857, 1]] const largestOfFour = (arr) => { return arr.map(items => Math.max(...items)) } console.log(largestOfFour(bigAssArr));

⚠️ According to @GBra's recommendation, Math.max(...arr) is not an optimal solution. ⚠️ 根据@GBra 的建议, Math.max(...arr)不是最优解。 It's slow, if the array contains too many elements it will fail or return the wrong result or hit max call stack size.它很慢,如果数组包含太多元素,它将失败或返回错误的结果或达到最大调用堆栈大小。

[Highly recommend !!!] Another option is to use .reduce to get the max item Of an array [强烈推荐!!!]另一种选择是使用.reduce来获取数组的最大项

 const bigAssArr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [100, 1001, 857, 1]] const getMaxItemOfArray = (arr) => arr.reduce((result, currentItem) => { return Math.max(currentItem, result); }, arr[0]); const result = bigAssArr.map(r => getMaxItemOfArray(r)); console.log(result);

You can find the largest in each array as follow.您可以在每个数组中找到最大的,如下所示。

 const bigAssArr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [100, 1001, 857, 1]] var biggestFour = bigAssArr.map( arr => { return Math.max(...arr); }) console.log(biggestFour)

 const myArr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [100, 1001, 857, 1]] let highest=arr=>arr.map(a=>Math.max(...a)) const highestValues=highest(myArr) console.log(highestValues)

Use map when you want to return a value for each element of an array如果要为数组的每个元素返回一个值,请使用 map

Use reduce when you want to return a single value based of the array, reduce will run the callback function for each element of the array, just like map当你想基于数组返回单个值时,使用reduce,reduce会为数组的每个元素运行回调function,就像map

reduce takes three arguments: reduce 需要三个 arguments:

  1. accumulator(this defaults to the first element in the array if no third argument is supplied, in the end reduce will return this value)累加器(如果没有提供第三个参数,则默认为数组中的第一个元素,最后 reduce 将返回此值)
  2. current value(this defaults to the second element in the array if no third argument is supplied, otherwise this starts from the first element)当前值(如果没有提供第三个参数,则默认为数组中的第二个元素,否则从第一个元素开始)
  3. starting accumulator value起始累加器值

If there is only one element in the array, reduce will have no effect.如果数组中只有一个元素,reduce 将不起作用。

 const bigAssArr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [100, 1001, 857, 1]] const largestOfFour = (arr) => { return arr.map((item) => { return item.reduce((acc, curr) => { if (acc > curr) { return acc; } return curr; }) }) } console.log(...largestOfFour(bigAssArr))

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

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