Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. I should have an example with different length.

like image 917
kay Avatar asked Oct 30 '25 13:10

kay


1 Answers

Use Array#reduce method with Array#forEach method.

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 :

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), [])
)


Using the simple for loop with the same logic.

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

like image 114
Pranav C Balan Avatar answered Nov 02 '25 02:11

Pranav C Balan