0

How to sort and get max value from 3 array of objects and assign the keys and values to another array?

I want to sort the arrays test1, test2, test3 and get the max score for a student and assign it to the array results as an object like

results = [{name: name, score: score}]

I am able to get the highest score out of all the tests but finding it a bit difficult to get the names and assigning it as an object to the results array

const test1 = [{name: 'vikash', score: 1}, {name: 'krish', score: 2}, {name: 'kunz', score: 3}]
const test2 = [{name: 'kunz', score: 4}, {name: 'vikash', score: 5}, {name: 'krish', score: 6}]
const test3 = [{name: 'krish', score: 7}, {name: 'kunz', score: 8}, {name: 'vikash', score: 9}]

const results = []

const maxScore = Math.max(Math.max(...test1.map(test => test.score)), Math.max(...test2.map(test => test.score)), Math.max(...test3.map(test => test.score)));

// prints highest score out of the 3 tests: 9
console.log(maxScore )

The results array should look like this at the end of it ..

resutls = [{name: 'vikash', score: 9}]

2 Answers 2

3
  1. Use the spread operator at parameter so that you can accept multiple arrays

    const topScore = (...arrays) => { ...
    
  2. Then wrap the arrays in a bracket and prefix with the spread operator and for good measure flatten the result

    [...arrays].flat()....
    
    // returns arrayOfObjects = [{object}, {object}, ....{object}]
    
  3. Finally, compare each objects score value (obj.value) in .reduce()

    arrayOfObjects.reduce((max, obj) => {
      return max.score < obj.score ? obj : max;
    ...
    

    The ternary compares the current obj.score vs. the obj with the greatest score from past iterations. The obj with the higher score stays or becomes max.

  4. BTW, I just noticed that the desired answer is the object within array. I have edited the demo to do so.


Demo

const test1 = [{name: 'vikash', score: 1}, {name: 'krish', score: 2}, {name: 'kunz', score: 3}];
const test2 = [{name: 'kunz', score: 10}, {name: 'vikash', score: 5}, {name: 'krish', score: 6}];
const test3 = [{name: 'krish', score: 7}, {name: 'kunz', score: 8}, {name: 'vikash', score: 9}];

const topScore = (...arrays) => {
  let result = [...arrays].flat().reduce((max, obj) => {
    return max.score < obj.score ? obj : max;
  });
  return [result];
};

console.log(topScore(test1, test2, test3));

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

Comments

1

You could do something like the following:

const test1 = [
  { name: "vikash", score: 1 },
  { name: "krish", score: 2 },
  { name: "kunz", score: 3 },
];
const test2 = [
  { name: "kunz", score: 4 },
  { name: "vikash", score: 5 },
  { name: "krish", score: 6 },
];
const test3 = [
  { name: "krish", score: 7 },
  { name: "kunz", score: 8 },
  { name: "vikash", score: 9 },
];

let results = [
  [...test1, ...test2, ...test3].reduce((p, c) => {
    return p.score > c.score ? p : c;
  }),
];

console.log(results);

Basically, you reduce the spreaded arrays and then for each object you check its score, if more than the previous then that's the highest and should be returned.

I've wrapped everything in an array so that the output is an array.

1 Comment

Yes, you definitely can use this for as many test arrays as you want, you just need to spread them.

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.