Find max value comparing multiple arrays for each index

前端 未结 6 1617
慢半拍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条回答
  •  Happy的楠姐
    2021-01-21 22:19

    You can combine Lo-Dash's zip and map methods to do this in just a few lines of code:

    var A = [[2.2, 3.3, 1.3], [1.2, 5.3, 2.2], [0.3, 2.2, 5.2]];
    
    // Creates an array of arrays, where the first array is all the first elements,
    // the second array is all the second elements, etc.
    var zipped = _.zip(A);
    var maxes = _.map(zipped, function(arr) {
        return _.max(arr);
    });
    console.log(maxes);
    

提交回复
热议问题