1

I was able to figure out how to return the highest number from a associative array with multiple objects. But I need the whole object.

I prepared this example:

var data = [
     { nr: 235, text: "foo" }
    ,{ nr: 351, text: "bar" }
];

var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
             return data[e]['nr'];
         }));
                
var index = "???";

console.log("Highest 'nr': " + highestNr);
console.log("Index at nr "+ highestNr + ": " + index);

//console.log(data[index]);

I need the index or the whole object. I need to show the text from the object with the highest number.

4
  • 5
    technical point, you haven't got an associative array here. You've got an array containing two objects. JS doesn't have associative arrays, it has objects - although it is possible to use them in a similar way to you might use an associative array in, say, PHP, e.g. by looping through the properties of the object. Commented Nov 13, 2018 at 12:58
  • Once you have the highest number can't you just filter the array on that value? Commented Nov 13, 2018 at 12:59
  • @DarrenSweeney: you could, but see the answer from NinaScholz for a better technique. Commented Nov 13, 2018 at 13:00
  • 1
    @ADyson, thanks for the hint. I changed the title of the question. Commented Nov 13, 2018 at 14:09

3 Answers 3

7

You could reduce the array by selecting the one with a greater value.

var data = [{ nr: 235, text: "foo" }, { nr: 351, text: "bar" }], 
    topNr = data.reduce((a, b) => a.nr > b.nr ? a : b);

console.log(topNr);

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

Comments

3

You can "sort" the array by "nr" property in descending order and get first element "[0]"

var data = [
     { nr: 235, text: "foo" }
    ,{ nr: 351, text: "bar" }
];

// slice added so that original data is not mutated
var result = data.slice(0).sort((a,b) => b.nr - a.nr)[0]

console.log(result)

3 Comments

Using sort is a good idea, especially if you also wanted the nth highest etc. But it might be worth pointing out this will mutate the array, so you might want to do [].concat(data).sort( if this is not desired.
Sorting is fine for small lists. But it's going to be O(n log n), whereas this can be done in O(n) as the reduce answers show.
@ScottSauyet Thanks for pointing complexity. Correct, it depends upon the size of array.
1

You can also use findIndex() method:

var data = [
     { nr: 235, text: "foo" }
    ,{ nr: 351, text: "bar" }
];

var highestNr = Math.max.apply(null, Object.keys(data).map(function(e) {
    return data[e]['nr'];
}));
                
var index = data.findIndex(function(ln) {
    return ln.nr === highestNr;
});

console.log("Highest 'nr': " + highestNr);
console.log("Index at nr "+ highestNr + ": " + index);

//console.log(data[index]);

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.