如何在数组的数组中获取相同索引处的最大值?

4
我有这样的数据:
data = [
   [{a: "b", value: 12}, {a: "bb", value: 39}, {a: "bb",  value: 150}],
   [{a: "c", value: 15}, {a: "cc", value: 83}, {a: "ccc", value: 12}],
   [{a: "d", value: 55}, {a: "dd", value: 9},  {a: "dd",  value: 1}]
]

我想要获取这些数据相同位置的最大值,所以我希望结果像这样:
[55, 83, 150]

目前,我可以获取对象中的每个值,并且如果指定索引,我可以获取最大值。但是我不确定如何在每个索引上执行此操作。

let array = [];
data.map((eachArr, index) => {
   array.push(eachArr[0].value)

   for(let i = 0; i < eachArr.length; i++){
     console.log('eachArr[i].value', eachArr[i].value, i);
   }
})
console.log(Math.max(...array)) ===> 55

我该怎么做呢?

我觉得人们误解了我的问题。我不想从每个数组中获取最大值。我想要具有相同索引的value的最大值。所以,我希望从12、15、55中获得55,从39、83、9中获得83,从150、12、1中获得150。非常抱歉我没有说清楚。我应该给出一个长度不同的示例。


那不是一个数组的数组,那是一组对象字面量的数组。 - max
2
@max 实际上,它是一个数组的数组(由对象组成)。 - user663031
2
@torazaburo 我觉得人们误解了我的问题。我不想要每个数组中的最大值。我想要每个数组中与value相同索引的最大值。所以我想要从12, 15, 55中得到55,从39, 83, 9中得到83,从150, 12, 1中得到150。对于没有说明清楚,我很抱歉。我应该有一个长度不同的例子。 - kay
3个回答

6
使用 Array#reduce 方法和 Array#forEach 方法。

var data = [[{ a: "b",value: 12}, { a: "bb",value: 39 }, { a: "bb", value: 150 }], [{   a: "c",   value: 15 }, {   a: "cc",   value: 83 }, {   a: "ccc",   value: 12 }], [{   a: "d",  value: 55 }, {   a: "dd",   value: 9 }, {   a: "dd",   value: 1 }]];


console.log(
  // iterate over the array
  data.reduce(function(arr, ele) {
    // iterate over the inner array
    ele.forEach(function(o, i) {
      // check element present at the index, if not then update with current value
      arr[i] = arr[i] || o.value;
      // check assign greatest value by comparing with previous value
      arr[i] = arr[i] < o.value ? o.value : arr[i];
      
      // you can combine above two lines
      // arr[i] = !arr[i] || arr[i] < o.value ? o.value : arr[i];
      
    });
    // return the array reference
    return arr;
    // set initial value as an empty array
  }, [])
)


// without any comments
console.log(
  data.reduce(function(arr, ele) {
    ele.forEach(function(o, i) {
      arr[i] = !arr[i] || arr[i] < o.value ? o.value : arr[i];
    });
    return arr;
  }, [])
)

使用ES6箭头函数:

使用ES6箭头函数

let data = [[{ a: "b",value: 12}, { a: "bb",value: 39 }, { a: "bb", value: 150 }], [{   a: "c",   value: 15 }, {   a: "cc",   value: 83 }, {   a: "ccc",   value: 12 }], [{   a: "d",  value: 55 }, {   a: "dd",   value: 9 }, {   a: "dd",   value: 1 }]];

console.log(
  data.reduce((arr, ele) => (ele.forEach((o, i) => arr[i] = !arr[i] || arr[i] < o.value ? o.value : arr[i]), arr), [])
)

使用简单的for循环,采用相同的逻辑。

var data = [[{ a: "b",value: 12}, { a: "bb",value: 39 }, { a: "bb", value: 150 }], [{   a: "c",   value: 15 }, {   a: "cc",   value: 83 }, {   a: "ccc",   value: 12 }], [{   a: "d",  value: 55 }, {   a: "dd",   value: 9 }, {   a: "dd",   value: 1 }]];

var res = [];

for (var i = 0; i < data.length; i++) {
  for (var j = 0; j < data[i].length; j++) {
    res[j] = !res[j] || res[j] < data[i][j].value ? data[i][j].value : res[j];
  }
} 

console.log(res);


提示:性能比较

这里提供了一个关于两个函数的性能比较。

3
这应该可以解决问题。
// Get the maxes from rows
 data.map(function(a){
    return Math.max.apply( null, a.map(function(b){ return b.value; }) );
 });

// Get maxes from cols
var maxes = data.map(function(v,i){
    return Math.max.apply( null, data.map(function(a){
       return a[i].value;
    }));
});
console.log("Maxes: ",maxes);
// Maxes:  [ 55, 83, 150 ]

0

在ES6中:

const data = [
   [{a: "b", value: 12}, {a: "bb", value: 39}, {a: "bb", value: 11}], 
   [{a: "c", value: 15}, {a: "cc", value: 83}, {a: "cc", value: 12}], 
   [{a: "d", value: 55}, {a: "dd", value:  9}, {a: "dd", value:  1}]
];    

const result = data[0].map((_, i) => 
  Math.max(...data.map(subarray => subarray[i].value)));

console.log(result);

请注意,如果子数组的长度不相同,则此方法将无法正常工作。
如果您更喜欢 ES5 版本:
const result = data[0].map(function(_, i) {
  return Math.max.apply(null, data.map(function(subarray) {
    return subarray[i].value;
  }));
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接