1

I have an array from the api response and its objects.I need to find the value which has maximum total and I have display it I'm unable to solve it , when there are two max values of the total.Is there any solution?

myArray[
{time: "2018-12-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-13-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-14-02", total: 35, percentage: 25, averageTime: 25},
{time: "2018-16-02", total: 65, percentage: 60, averageTime: 20},
{time: "2018-17-02", total: 40, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 65, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 35, percentage: 23, averageTime: 20}
];

I tried reduce function like this:

var data =[];
data.reduce((max, p) => p.total > max ? p.total : max, data[0].total);
7
  • Can you explain it please? Commented Mar 14, 2018 at 6:51
  • What do you mean by 'when there are two max values of the total'? Commented Mar 14, 2018 at 6:54
  • when ther are two same values of object 'total' . Commented Mar 14, 2018 at 6:55
  • What do you want to display when there are two maximums? Both or either one or the earlier one or the later one? What if there are three, four? Or do you just need the max total value? Commented Mar 14, 2018 at 6:56
  • I need to display the time of when two objects has same highest 'total' value. Commented Mar 14, 2018 at 6:57

4 Answers 4

2

You can do something like this:

Idea:

  • Create a hashMap with total as keys and a variable to hold max value.
  • Check if the key exists and if not, initialize it to an array.
  • Push current object in this array.
  • Fetch the array with the key as max and you will get all the values with max total.

Now that you have entire object, you can iterate and display any necessary values.

var myArray = [{time: "2018-12-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-13-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-14-02", total: 35, percentage: 25, averageTime: 25},
{time: "2018-16-02", total: 65, percentage: 60, averageTime: 20},
{time: "2018-17-02", total: 40, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 65, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 35, percentage: 23, averageTime: 20}]

var max = 0;
var hashMap = myArray.reduce((acc, p) => {
  max = Math.max(max, p.total);
  acc[p.total] = acc[p.total] || [];
  acc[p.total].push(p);
  return acc;
}, {});

console.log(hashMap[max]);

With only time value

var myArray = [{time: "2018-12-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-13-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-14-02", total: 35, percentage: 25, averageTime: 25},
{time: "2018-16-02", total: 65, percentage: 60, averageTime: 20},
{time: "2018-17-02", total: 40, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 65, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 35, percentage: 23, averageTime: 20}]

var max = 0;
var hashMap = myArray.reduce((acc, p) => {
  max = Math.max(max, p.total);
  acc[p.total] = acc[p.total] || [];
  acc[p.total].push(p.time);
  return acc;
}, {});

console.log(hashMap[max]);

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

2 Comments

Instead of getting the full object,Can you help to get the 'time' alone?
Kindly check the update. Though I prefer getting entire object. This way, its more flexible.
1

You can use Math.max along with spread and array.prototype.map:

var myArray = [
{time: "2018-12-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-13-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-14-02", total: 35, percentage: 25, averageTime: 25},
{time: "2018-16-02", total: 65, percentage: 60, averageTime: 20},
{time: "2018-17-02", total: 40, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 65, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 35, percentage: 23, averageTime: 20}
];

var max = Math.max(...myArray.map(e => e.total));

console.log(max);

Comments

0

You can find the maximum 'total' value in the array and then find all the elements of the array with that maximum value.

var myArray = [
{time: "2018-12-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-13-02", total: 25, percentage: 20, averageTime: 20},
{time: "2018-14-02", total: 35, percentage: 25, averageTime: 25},
{time: "2018-16-02", total: 65, percentage: 60, averageTime: 20},
{time: "2018-17-02", total: 40, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 65, percentage: 20, averageTime: 20},
{time: "2018-18-02", total: 35, percentage: 23, averageTime: 20}
];

var max = Math.max(...myArray.map(e => e.total));
var time = myArray.filter(item => item.total == max);

console.log(time);

1 Comment

You are looping twice. You can achieve this in same loop. Feel free to refer to my answer.
0

One possible approach:

function getMaxValue(myArray) {
    var totals = myArray.map(x => x.total);
    var max = filter(x => x == Math.max(totals));
    return max.length == 1 ? max[0] : 'unable to solve it';
}

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.