To find the maximum y value of the objects in array:
Math.max.apply(Math, array.map(function(o) { return o.y; }))
or in more modern JavaScript:
Math.max(...array.map(o => o.y))
Warning:
This method is not advisable, it is better to use reduce. With a large array, Math.max will be called with a large number of arguments, which can cause stack overflow.
To find the maximum y value of the objects in array:
Math.max.apply(Math, array.map(function(o) { return o.y; }))
or in more modern JavaScript:
Math.max(...array.map(o => o.y))
Warning:
This method is not advisable, it is better to use reduce. With a large array, Math.max will be called with a large number of arguments, which can cause stack overflow.
Find the object whose property "Y" has the greatest value in an array of objects
One way would be to use Array reduce..
const max = data.reduce(function(prev, current) {
return (prev && prev.y > current.y) ? prev : current
}) //returns object
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
- http://caniuse.com/#search=reduce (IE9 and above)
If you don't need to support IE (only Edge), or can use a pre-compiler such as Babel you could use the more terse syntax.
const max = data.reduce((prev, current) => (prev && prev.y > current.y) ? prev : current)
Note that if you have multiple objects sharing the same max value, it will only return the first matched object.
ES6 Find the maximum number of an array of objects?
If you want to use ES6 and get this done in one line:
const shots = [
{id: 1, amount: 2},
{id: 2, amount: 4},
{id: 3, amount: 52},
{id: 4, amount: 36},
{id: 5, amount: 13},
{id: 6, amount: 33}
];
shots.reduce((acc, shot) => acc = acc > shot.amount ? acc : shot.amount, 0);
Can replace the ternary with a Math.max(acc, shot.amount), along with many other multiple methods. The issue with the reduce function you produced on SO, your reducer accumulator/initial value (0) was placed in the wrong spot.
More on reddit.comHow can I easily obtain the min or max element of a JavaScript array? - TestMu AI Community
Map or Reduce to calculate max value from JSON
javascript - How to do reduce math.max to an array of objects - Stack Overflow
Videos
Hi Reddit,
I have question about a very simple problem, I'm not looking for lines of code, but more for an explanation about way programmers seem to think in a certain way.
I'm not new to programming, but I'm new to javascript so I sometimes wonder if I can do thing better.
My problem: I have a simple JSON object and I want to calculate the max value for a certain field (val).
var data = {
2013: [
{
prov: "salerno",
val: 100
},
{
prov: "napoli",
val: 35
},
{
prov: "avellino",
val: 0
}
]
};
array = data[2013];My initial thought was I have a bunch of values and I want only one (the max) so I will use a reduce function. So I wrote this:
array.reduce(function(max, x) { return (x.val > max) ? x.val : max; }, 0)I was pretty happy with my code, then I checked on stackoverflow to see if there is a better way to do it and I found this:
Finding the max value of an attribute in an array of objects
The right answer with a lot of upvotes was:
Math.max.apply(Math,array.map(function(o){return o.y;}))While I understand the code and how it works (get an array from the JSON and apply the Math.max()), I'm not sure way all the people in StackOverflow went for a solution with map() (to extract the array) instead to go for a solution with reduce() (to have a single value).
Can anyone help me to understand better?
Done by simple reduce,
var arr=[{"number":1000,"name":"Josh"},{"number":2000,"name":"Joker"},{"number":3000,"name":"Batman"}]
var x = arr.reduce((acc, i)=>(i.number > acc.number ? i : acc))
console.log(x)
With Array.prototype.reduce, you have to remember that the first argument passed to the callback is the accumulator. That is, the return value from the previous iteration.
Math.max returns a Number (or NaN if the operands cannot be compared numerically) so on the second iteration, you will be trying to compare the number property of a Number which won't work.
As an alternative to reduce, you might want to consider sorting the array. This could be beneficial if you want other properties like the smallest value.
// smallest to largest, use "b.number - a.number" to reverse
arr.sort((a, b) => a.number - b.number)
arr[0] // smallest
arr[arr.length - 1] // largest