[英]How to get max value at the same index in array of arrays?
I have data like this: 我有这样的数据:
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}]
]
I want to get the largest value at the same index in this data, so I want the result to be like this: 我想在这个数据中获得相同索引的最大值,所以我希望结果如下:
[55, 83, 150]
Right now, I can get each value in objects, and I can get the largest value if I specify the index. 现在,我可以获取对象中的每个值,如果指定索引,我可以获得最大值。 I am not sure how to do it with every index.
我不确定如何使用每个索引。
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
How can I do this? 我怎样才能做到这一点?
I think people misunderstood my question. 我认为人们误解了我的问题。 I do not want the largest value from each array.
我不想要每个数组的最大值。 I want the largest value of
value
with the same index in every array. 我希望每个数组中具有相同索引的最大值的
value
。 So I want 55 from 12, 15, 55, 83 from 39, 83, 9, and 150 from 150, 12, 1. I am sorry for not being specific. 所以我希望55,13,55,83分别来自39分,83分,9分和150分,分别是150分,12分,1分。我很抱歉没有具体。 I should have an example with different length.
我应该有一个不同长度的例子。
Use Array#reduce
method with Array#forEach
method. 对
Array#forEach
方法使用Array#reduce
方法。
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; }, []) )
With ES6 arrow function : 使用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), []) )
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);
FYI : Performance comparison 仅供参考: 绩效比较
This should do it. 这应该做到这一点。
// 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 ]
In ES6: 在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);
Note that this will not work properly if the subarrays are not of the same length. 请注意,如果子数组的长度不同,则无法正常工作。
If you prefer an ES5 version: 如果您更喜欢ES5版本:
const result = data[0].map(function(_, i) {
return Math.max.apply(null, data.map(function(subarray) {
return subarray[i].value;
}));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.