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?
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 .
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.
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);
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With