javascript 返回数组中的最大数字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 返回数组中的最大数字相关的知识,希望对你有一定的参考价值。

/*------------------------------BASIC------------------------------------------------------------------*/


function largestOfFour(arr) {
var largestNum = [0,0,0,0];//Создаем пустой еррей с обозначением количества и типа нужных данных - чисел
  
  for(var i= 0; i<arr.length; i++)//{arr[i] = любой из четырех саберрев/arr[i][j] - каждое чило в нем
    
    for(var j= 0; j<arr[i].length; j++){
      
      if(largestNum[i]<arr[i][j]){//largestNum[i] - один из замещаемых нулей, arr[i][j] - луп по каждому числу
        
        largestNum[i]=arr[i][j];
      }
    }
    
  }
  
  return largestNum;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

/*---------------------------------------Intermediate------------------------------------------------*/


function largestOfFour(arr) {
return arr.map(function(subArray){//Сразу ретерним результат аргуменетом перебора делаем сабаррей, то есть 

arr[i];
  
  return subArray.reduce(function(val1, val2){//В сабаррее ныряем в его значения
    
    return (val1>val2)?val1:val2;// Тренарній оператор по сравнению и назначению большего из чисел
    
  });
  
}, 0);
}



largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

/*--------------------------------Medium----------------------------------------------------------------------------------*/

function largestOfFour(mainArray) {
  // Step 1. Map over the main arrays
  return mainArray.map(function(subArray) { // Step 3. Return the largest numbers of each sub-arrays => returns [5,27,39,1001]
    
    // Step 2. Return the largest numbers for each sub-arrays with Math.max() method
    return Math.max.apply(null, subArray);
  });
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Approach #3: Return the Largest Numbers in a Array With Built-In Functions — with map() and apply()
For this solution, you’ll use two methods: the Array.prototype.map() method and the Function.prototype.apply() method.

The apply() method calls a function with a given this value and arguments provided as an array (or an array-like object).
You can pass an array of arguments to a function by using the apply() method and the function will execute the items in the array.

Such functions are known as variadic functions, and they can accept any number of arguments instead of a fixed one.

The Math.max() function returns the largest of zero or more numbers, and we can pass any number of arguments.

console.log(Math.max(4,5,1,3)); // logs 5
But you can’t pass an array of numbers to the method like this​:

var num = [4,5,1,3];
console.log(Math.max(num)); // logs NaN
This is where the apply() method turns out to be useful:

var num = [4,5,1,3];
console.log(Math.max.apply(null, num)); // logs 5
Note that the first argument to apply() sets the value of ‘this’, not used in this method, so you pass null.

Now that you have a method to return the largest number in a array, you can loop through each sub-arrays with the map() method and return all largest numbers.

以上是关于javascript 返回数组中的最大数字的主要内容,如果未能解决你的问题,请参考以下文章

javascript [最大的4个]用于返回数组中最大数字的脚本#vanilla #script

使返回数组中的最大数值,javascript [重复]

从Javascript中的数字数组返回坐标(x,y)

JavaScript3.0

通过递归分而治之的数组中的最大数字

747. 至少是其他数字两倍的最大数