Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find max value in object array [duplicate]

I have object that looks like this:

peaks = 
0: {intervalId: 7, time: 1520290800000, value: 54.95125000000001}
1: {intervalId: 7, time: 1520377200000, value: 49.01083333333333}

and so on.

How do I find peak that has Max value? I tried to do it like this

this.loadPeak = peaks.map(a => Math.max(a.value));

but I just got bunch of peaks array with value (instead of all intervalId, time, value) and not the max value.

**Thank you so much for everyone, every solution was working, sadly can't accept all. **

like image 764
user122222 Avatar asked Oct 21 '18 14:10

user122222


People also ask

How to get MAX VALUE in array of objects in JavaScript?

Use inbuilt method Math apply (), map (), reduce () to get the max value in Array of objects in JavaScript. The Array object lets you store multiple values in a single variable. Ways to get max value in Array of objects:- Using apply () and map () method

How do you find the maximum number in an array?

This method is the traditional way to find the maximum number from an array. It includes an iterator that is used to go through every element in the array. Below, we have an array of integers, intArray; first, we create a variable maxNum and initialize it with the first element of intArray.

How to get the Min and max values in an object?

To get the min and max values in an Object: Use the Object.values method to get an array of the object's values. Using spread syntax, pass the array as an argument to the Math.min and Math.max methods. The Math.min and Math.max methods return the lowest and highest of the passed in numbers.

How to find the maximum value in a stream in Java?

One of them is the Arrays.stream () method that takes an array and returns a sequential stream. In our case, we have an array of the int type, and when we pass it in the stream, it returns an IntStream. The IntStream function comes with a method max () that helps to find the maximum value in the stream.


3 Answers

The main issue with sorting your array, is it causes many needless iterations through your array. This gets drastically slower the bigger your array is, sorting to try and move elements up and down. With reduce(), we can handle this in the minimum amount of steps needed, simply replacing the previous value if the current element's value is greater than the previous:

var peaks = [
  {intervalId: 7, time: 1520290800000, value: 54.95125000000001},
  {intervalId: 7, time: 1520377200000, value: 49.01083333333333}
];

const maxPeak = peaks.reduce((p, c) => p.value > c.value ? p : c);

console.log(maxPeak);
like image 138
ugh StackExchange Avatar answered Oct 09 '22 08:10

ugh StackExchange


You could spread only the value value for Math.max.

var peaks = [{ intervalId: 7, time: 1520290800000, value: 54.95125000000001 }, { intervalId: 7, time: 1520377200000, value: 49.01083333333333 }]
    max = Math.max(...peaks.map(({ value }) => value)),
    object = peaks.find(({ value }) => value === max);

console.log(max);
console.log(object);
like image 29
Nina Scholz Avatar answered Oct 09 '22 08:10

Nina Scholz


The simple way is just to use a loop:

this.loadPeak = null;
for (const peak of peaks) {
    if (!this.loadPeak || peak.value > this.loadPeak.value) {
        this.loadPeak = peak;
    }
}

Live Example:

const peaks = [
  {intervalId: 7, time: 1520290800000, value: 54.95125000000001},
  {intervalId: 7, time: 1520377200000, value: 49.01083333333333}
];

let loadPeak = null;
for (const peak of peaks) {
    if (!loadPeak || peak.value > loadPeak.value) {
        loadPeak = peak;
    }
}
console.log(loadPeak);

As with nearly any array operation, you can shoe-horn it into a reduce call if you like:

this.loadPeak = peaks.reduce((maxPeak, peak) => !maxPeak || maxPeak.value < peak.value ? peak : maxPeak, null);

const peaks = [
  {intervalId: 7, time: 1520290800000, value: 54.95125000000001},
  {intervalId: 7, time: 1520377200000, value: 49.01083333333333}
];

const loadPeak = peaks.reduce((maxPeak, peak) => !maxPeak || maxPeak.value < peak.value ? peak : maxPeak, null);
console.log(loadPeak);

I should have realized earlier this was a duplicate question. I've found the dupetarget, marked it, and made this a CW answer.

like image 2
3 revs Avatar answered Oct 09 '22 07:10

3 revs