3

I know a similar question has been asked here: Finding the max value of an attribute in an array of objects, but with that method there is no way to return the entire object containing the maximum.

I have this array of objects:

[
 {Prop: "something", value: 2},
 {Prop: "something_else", value: 5},
 {Prop: "bla", value: 3}
]

I want to find the maximum value over the property "value" and then I want to return the entire object

{Prop: "something_else", value: 5}

What is the easiest way to do that in javascript?

Community
  • 1
  • 1
valenz
  • 35
  • 3
  • 1
    Just loop through the array. – Aravind Nov 12 '14 at 19:48
  • It's not enough. I would have done it otherwise. – valenz Nov 12 '14 at 20:00
  • Why is it not enough? – Aravind Nov 12 '14 at 20:01
  • Better: I don't know how to find the maximum and keep track of the object within a loop. – valenz Nov 12 '14 at 20:05
  • Oh, see my answer, it's the easiest way, loop through elements and keep track of maximum and return that object. – Aravind Nov 12 '14 at 20:10
  • you can also pluck a property from each object element to create an index, which you can use [].indexOf() on to very quickly get the array position of an object with the specified property. that's nice because it's often faster and always more flexible to not hard-code the property name into the iteration workload. – dandavis Nov 12 '14 at 20:57

5 Answers5

3

Could also use something like Array.prototype.reduce.

i.e.

function maxVal(arr) {
    return arr.reduce(function (prev, curr) {
        return (prev.value >= curr.value) ? prev : curr;
    });
}

EDIT: If you want a basic way of doing it, simply by iterating through the array, you can do something like:

function maxVal(arr) {
    var max = arr[0];
    for (var i = 1, iMax = arr.length; i < iMax; i++) {
        if (max.value < arr[i].value) {
            max = arr[i];
        }
    }
    return max;
}
Blue
  • 22,608
  • 7
  • 62
  • 92
boxsome
  • 178
  • 5
  • The first one works. The simpler one does not. There is no trace of "value". Thanks anyway. – valenz Nov 12 '14 at 20:19
3

The easiest way is to do this 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:

const objs = [
 {Prop: "something", value: 2},
 {Prop: "something_else", value: 5},
 {Prop: "bla", value: 3}
];

const maxObj = objs.reduce((p, c) => p.value > c.value ? p : c);

console.log(maxObj);

Don't sort your array! The main issue with sorting an array, is it causes many needless iterations through. This gets drastically slower the bigger your array is, sorting to try and move elements up and down.

Blue
  • 22,608
  • 7
  • 62
  • 92
0

Try this:

array.slice().sort(function(a, b) { return a.value < b.value; })[0];

(slice clones the array)

friedi
  • 4,350
  • 1
  • 13
  • 19
0

If A is the array of objects.

function return_Max_Object(A)
{
 var M = -Infinity;
 var Max_index = -1;
 for(var i =0;i<A.length;i++)
 {
  if(A[i].value>M) 
  {
    M = A[i].value;
    Max_index = i;
  }
 }
 return A[Max_index];
}
Aravind
  • 3,169
  • 3
  • 23
  • 37
0
function getMaxByVal(objArr){
    var max=objArr[0]["value"];
    var indexStore=0;
    var  i=0;
    for(len=objArr.length; i<len;i++){
        if(max < objArr[i]["value"]){
            max=objArr[i]["value"];
            indexStore=i;
        }
    }
    return objArr[indexStore]["value"]
}
years_of_no_light
  • 938
  • 1
  • 10
  • 24