I know there are similar questions here, but with those methods only one max value is returned. What I need is to determine which objects of the array have that max value in a given property and return the value of a certain (other) property within those objects that have the max value in the given property.
I have this array of objects called week with two properties "name" and "traffic" :
[
{ name: "Saturday", traffic: 12 },
{ name: "Sunday", traffic: 12 },
{ name: "Monday", traffic: 13 },
{ name: "Tuesday", traffic: 9 },
{ name: "Wednesday", traffic: 10 },
{ name: "Thursday", traffic: 8 },
{ name: "Friday", traffic: 13 },
]
In this case Monday and Friday have the max value for the property "Traffic" which is 13 and I need a way to return a string containing the name of the day with highest "Traffic" value if there is only one day, and an array containing the names (as strings) of the days that have highest "Traffic" value if there are more than one day with highest "Traffic" value, as in this case would return an array containing Monday and Friday.
I have tried this:
function getMaxTr() {
return week.reduce((max, p) => p.traffic > max ?
p.traffic : max, week[0].traffic);
}
But with this I only got one max value of the property "traffic" which is 13.
And this:
let max = week [week.length - 1];
With this last one I get one object which has the max traffic value, like this:
Object { name: "Friday", traffic: 13 }