Find max value comparing multiple arrays for each index

前端 未结 6 1568
慢半拍i
慢半拍i 2021-01-21 21:58

I\'m trying to find a method to find the max value comparing multiple(unknown number, but same length) arrays for each observation in the arrays, returning an array with the max

6条回答
  •  故里飘歌
    2021-01-21 22:09

    For each index of the array, create an array containing all elements in the "column" and find the max of those values. Return the generated array. Sample usage: maxValues(A) would give the desired result.

    function maxValues(array) {
        var maxArray = [];
        var length = array[0].length;
        for (var i = 0; i < length; i++) {
            var ithColumn = [].map.call(array, function(array) {
                return array[i];
            });
            maxArray.push(Math.max.apply(null, ithColumn));
        }
        return maxArray;
    }
    

提交回复
热议问题