Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get max values from each array of objects

So 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 max values of value in each array of objects. So the result should be like this:

maxValues = [150, 83, 55]

Right now my code is:

let maxValues = []
let tmp;
let highest = Number.NEGATIVE_INFINITY;

data.map((eachArr, index) => {
  for(let i = eachArr.length -1; i >= 0; i--){
    tmp = eachArr[i].value;
    if(tmp > highest) highest = tmp;
  }
  maxValues.push(highest)
})

and the result is

maxValues = [150, 150, 150]

How can I achieve this?

like image 878
kay Avatar asked Jan 23 '17 08:01

kay


People also ask

How do you find the maximum value of an array of objects in Java?

You can use Collections. max as suggested by Steve Kuo, or in Java 8, use Arrays. stream to convert the array into a stream, then call Stream. max .

Can math Max take an array?

Math. max() can be used to find maximum element of a javascript array. But you cannot directly pass an array as argument to max() as it will return NaN . For using array with max() , you need to destructure it using javascript spread operator, which extracts individual elements from the array.


3 Answers

You could use the spread syntax and take the max of mapped value.

The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables  (for destructuring assignment) are expected.

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 }]],
    result = data.map(a => Math.max(...a.map(b => b.value)));

console.log(result);
like image 169
Nina Scholz Avatar answered Oct 17 '22 20:10

Nina Scholz


You have to include let highest = Number.NEGATIVE_INFINITY; inside forEach function, because you have to set highest from every set in your array.

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}]
       ]
let maxValues = []
let tmp;
data.forEach((eachArr, index) => {
  let highest = Number.NEGATIVE_INFINITY;
  for(let i = eachArr.length -1; i >= 0; i--){
    tmp = eachArr[i].value;
    if(tmp > highest) highest = tmp;
  }
  maxValues.push(highest)
})
console.log(maxValues);
like image 45
Mihai Alexandru-Ionut Avatar answered Oct 17 '22 21:10

Mihai Alexandru-Ionut


Ecmascript 5 solution with Math.max.apply() and Array.prototype.map() functions:

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 maxValues = data.map(function (arr) {
    return Math.max.apply(null, arr.map(function(o){ return o.value; }));
});

console.log(maxValues);

Math.max.apply() function will return the largest of the given numbers.
The numbers(value attribute of each nested object) are accumulated via Array.prototype.map() function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

like image 4
RomanPerekhrest Avatar answered Oct 17 '22 22:10

RomanPerekhrest