Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all objects with Max value in a property within an Array of Objects and return values of other property from the same object

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 }

like image 998
Samwise Avatar asked Oct 17 '25 15:10

Samwise


2 Answers

You can use reduce. In every iteration, check if there's either an element with a lower or an equal traffic property in your result, if so either replace the whole thing for the former case, or add the equal element to your result. If none of above returns true, simply return the last iteration's element again.

const arr = [
    { 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 },
];

let res = arr.reduce((a, b) => {
    let now = a.pop();
    if (now.traffic < b.traffic) return [b];
    if (now.traffic === b.traffic) return [...a, now, b];
    return [...a, now];
}, [arr[0]]).map(e => e.name);

res = res.length > 1 ? res : res[0];

console.log(res);
like image 68
baao Avatar answered Oct 20 '25 03:10

baao


Well if you want to return the name of the objects with max traffic value, you can use a combination of Array#filter(), Array#reduce() and Array#map() methods like this:

let maxTraffic = arr.reduce(function(a, b) {
    return a.traffic > b.traffic ? a.traffic : b.traffic;
});
var result = arr.filter(a => a.traffic == maxTraffic).map(a => a.name);

This will return an array containing the names of elements with max traffic value.

Demo:

This is a working demo:

var arr = [
 { 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 },
];

let maxTraffic = arr.reduce(function(a, b) {
    return a.traffic > b.traffic ? a.traffic : b.traffic;
});
var result = arr.filter(a => a.traffic == maxTraffic).map(a => a.name);

console.log(result);
like image 29
cнŝdk Avatar answered Oct 20 '25 03:10

cнŝdk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!