0

How to sort and get max score for each name from 3 array of objects in javascript and assign it and the missing keys and values to another array?

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

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

With the following code I am able to get the object with the highest score like:

resutls = [
       { name: 'sam', age: 15, score: 30 }
    ]

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

    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));

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

resutls = [
   { name: 'vikash', age: '', score: 10 },
   { name: 'sam', age: 15, score: 30 },
   { name: 'krish', age: '', score: 7 },
   { name: 'kunz', age: 10, score: 8 },
]
1
  • Can you explain what exactly in that expected result is supposedly “sorted”? Commented Apr 22, 2020 at 11:16

2 Answers 2

1

You could group by name and get all values from the object with the max score objects.

const
    test1 = [{ name: 'vikash', score: 1 }, { name: 'krish', score: 2 }, { name: 'kunz', score: 3 }],
    test2 = [{ name: 'kunz', score: 0 }, { name: 'vikash', score: 5 }, { name: 'krish', score: 6 }, { name: 'sam', age: 15, score: 30 }],
    test3 = [{ name: 'krish', score: 7 }, { name: 'kunz', age: 10, score: 8 }, { name: 'vikash', score: 10 }, { name: 'sam', score: '' }],
    result = Object.values([...test1, ...test2, ...test3].reduce((r, o) => {
        if (!r[o.name] || r[o.name].score < o.score) {
            r[o.name] = { ...(r[o.name] || { name: '', age: '', score: 0 }), ...o };
        }
        return r;
    }, {}));

console.log(result);

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

Comments

0

You just select the maximum of all the results, to get an array you can build a dictionary of result by name and then select all the maximums:

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

    const topScore = (...arrays) => {
        let result = [...arrays].flat().reduce((max, obj) => {
          //check if current item or maximum item contains an age.  
        let age = obj.age || (max[obj.name] || {age: ''}).age
            return (max[obj.name] || {score: 0}).score < obj.score ? {...max,[obj.name]: {...obj , age}} : {...max }
        }, {});
        return Object.values(result)
    };

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

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.