1

Let's say I have the following array of objects:

[
   { name: 'january', score: 3.02 },
   { name: 'february', score: 1.02 },
   { name: 'march', score: 0 },
   { name: 'april', score: 12 },
]

What would be the quickest method for extract the position (index) of the element of the object with the highest score value...so, in the above case, the value would be index 3...

N.B. Scores are dynamic, and the "winning" element is the highest value...

5

3 Answers 3

2

You could get the keys and reduce the indices by checking the score.

var data = [{ name: 'january', score: 3.02 }, { name: 'february', score: 1.02 }, { name: 'march', score: 0 }, { name: 'april', score: 12 }],
    index = [...data.keys()].reduce((a, b) => data[a].score > data[b].score ? a : b);

console.log(index);

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

4 Comments

In your above example, what are you expecting the var index to be?
i expect 3, because it has the hightest score of 12.
In my real world example, I always get 11 (which is the last element in the list). Perhaps because the scores are logged as floats?
Nope, apologies, this works! Being stupid - my key is fuzzyRegexScore and not score - a quick edit and boom! Works. Thank you.
1

Try this. Use javascript max and map function to get value of index

 var data = [
   { name: 'january', score: 3.02 },
   { name: 'february', score: 1.02 },
   { name: 'march', score: 0 },
   { name: 'april', score: 12 }
];

var maxValue = 
Math.max.apply(Math, data.map(function(row,index) { return index; }))


console.log(maxValue)

2) I think this will also give you correct result

var data = [
   { name: 'april', score: 1 },
   { name: 'january', score: 3.02 },
   { name: 'february', score: 11.02 },
   { name: 'march', score: 2 }
   
];
var maxValue = Math.max.apply(Math, data.map(function(row) { return row.score; }))
var key = data.findIndex((row,index)=>{ if(row.score ===maxValue){return true}})

console.log(key)

3 Comments

This is the maxValue of the scores, I want to index of the highest scoring object. Thank you for the idea tho.
i have returned index from map not score try running code snippet
Hmmm, maybe because I have floats or something ... but using your example always returns 11.
1

var data = [
   { name: 'january', score: 3.02 },
   { name: 'february', score: 1.02 },
   { name: 'march', score: 0 },
   { name: 'april', score: 12 },
];

var resultValue = null;
var tempValue = Number.NEGATIVE_INFINITY;
data.forEach(function(element, index) {
    if(element.score > tempValue) {
        tempValue = element.score;
        resultValue = index;
    }
});

console.log(resultValue);

3 Comments

Since OP didn't state scores can't be negative I wouldn't make the assumption. There's a value for that.
That's not the value: Number.NEGATIVE_INFINITY
This is great, and works. I'll let the communtiy vote to see if this should be the ideal winner over time. Thanks @caramba

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.