4

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 }

1
  • 3
    What have you tried? Please post your code. Commented Jul 18, 2018 at 14:39

3 Answers 3

3

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Probably the best algorithm, but a few checks and return the right thing is not an explanation.
3

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);

Comments

2

You can use the function reduce to group the days and the function pop in case the reduce array contains only one element.

var array = [ { 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 }],
    reduced = array.reduce((a, {name, traffic}) => {
      if (traffic > a.highest) {
        a.current = [name];    
        a.highest = traffic;    
      } else if (traffic === a.highest) a.current.push(name);
  
      return a;
    }, {highest: 0, current: []}).current,
    result = reduced.length === 1 ? reduced.pop() : reduced;

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

This code snippet shows the result as String when the sample contains only one object with the highest traffic value:

var array = [ { name: "Saturday", traffic: 12 }, { name: "Sunday", traffic: 12 }, { name: "Monday", traffic: 1 }, { name: "Tuesday", traffic: 9 }, { name: "Wednesday", traffic: 10 }, { name: "Thursday", traffic: 8 }, { name: "Friday", traffic: 13 }],
    reduced = array.reduce((a, {name, traffic}) => {
      if (traffic > a.highest) {
        a.current = [name];    
        a.highest = traffic;    
      } else if (traffic === a.highest) a.current.push(name);
  
      return a;
    }, {highest: 0, current: []}).current,
    result = reduced.length === 1 ? reduced.pop() : reduced;

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.