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 },
]