0

My array looks like this:

var points = [{category: "Category A", correct: "0"},{category: "Category B", correct: "4"}];

I am trying to see which category has the highest number of correct. I started by using Math.max on my array map to determine which one is the highest value (which it returns 4.) But when I try to expand on that return it begins defaulting to NaN.

var res = Math.max.apply(Math,points.map(function(k){
    return k.correct;
}));

o.correct in this instance will return 4.

JSFiddle


What I've tried:

  1. Switching my return to k.category

    -> This returns "NaN"

  2. Treating it as a function and storing both variables in the Math.max, this will pull both values of the object, but it does it for both objects in the array.

    Math.max.apply(Math,points.map(function(k){ var highestCat = k.category; var highestCor = k.correct; alert(highestCat + highestCor); }));

Any ideas on what I could do to be able to store these as 2 separate variables I could reference later on?

3 Answers 3

1

You can sort the array and grab the first element

var points = [{category: "Category A", correct: "0"},{category: "Category B", correct: "4"}];
var sorted = points.sort((a,b)=>b.correct - a.correct)
var highest = sorted[0];
alert(highest.category + highest.correct);

same thing with object destructuring

var points = [{category: "Category A", correct: "0"},{category: "Category B", correct: "4"}];   
var {category:highestCateogry,correct:highestCorrect} = points.sort((a,b)=>b.correct - a.correct)[0];
alert(highestCateogry + highestCorrect); 
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't think about sorting, but after trying to plug this in, it seems to only return [object Object]. Check this fiddle. Edit: just saw your edit to the answer.
When you alert a object it implicitly calls the objects toString method. sorted is an array, the default toString on most objects is [object Object], highest is the first element in the sorted array {category:"Category B", correct: "4"} again uses the default toString of [object Object] since it does not define it's own toString function
Using reduce(or any other loops) is faster than sorting
1

You can use Array reduce

let points = [{category: "Category A", correct: "0"},{category: "Category B", correct: "4"},{category: "Category C", correct: "1"},{category: "Category D", correct: "5"},{category: "Category E", correct: "0"}];


const max = points.reduce((currentMax,newMax) => { 
return +currentMax.correct  < +newMax.correct ?  newMax :currentMax
})

console.log(max)

Comments

0

You have NaN because correct is a string and not a number. Use parseInt function to convert the string into integer

1 Comment

I'd like to keep it a string so I can compare the value in conditionals later on. ie: if highestCat = category a

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.