JavaScript finding the largest integer in an array of arrays

后端 未结 5 464
独厮守ぢ
独厮守ぢ 2021-01-23 21:30
function largestOfFour(arr) {
    var newArray = [];
    for(var i =0; i <=arr.length-1; i++){
        console.log(arr[i]);
        newArray[i] = Math.max(arr[i]);
           


        
5条回答
  •  無奈伤痛
    2021-01-23 21:58

    You are passing an array to Math.max and expect it to return the maximum in that array.

    However, Math.max returns the maximum among its arguments. So use

    var newArray = [];
    for(var i =0; i < arr.length; ++i)
      newArray[i] = Math.max.apply(void 0, arr[i]);
    

    In ES6, you can use arrow functions and the spread operator to simplify:

    arr.map(a => Math.max(...a));
    

提交回复
热议问题