![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript R- eturn Array Item(s) With Largest Score
We have an array of arrays that contains the marks scored by some students in some subjects −
const arr = [ ['Math', 'John', 100], ['Math', 'Jake', 89], ['Math', 'Amy', 93], ['Science', 'Jake', 89], ['Science', 'John', 89], ['Science', 'Amy', 83], ['English', 'John', 82], ['English', 'Amy', 81], ['English', 'Jake', 72] ];
We are required to write a function that takes in this array and retuns an array of object, with one object for each subject and the details about the top scorer of that subject.
Our output should look like −
[ { "Subject": "Math", "Top": [ { Name: "John", Score: 100} ] }, { "Subject": "Science", "Top": [ { Name: "Jake", Score: 89}, { Name: "John", Score: 89} ] }, { "Subject": "English", "Top": [ { Name: "John", Score: 82} ] } ]
Let's write the code for this function −
Example
const arr = [ ['Math', 'John', 100], ['Math', 'Jake', 89], ['Math', 'Amy', 93], ['Science', 'Jake', 89], ['Science', 'John', 89], ['Science', 'Amy', 83], ['English', 'John', 82], ['English', 'Amy', 81], ['English', 'Jake', 72] ]; const groupScore = arr => { return arr.reduce((acc, val, index, array) => { const [sub, name, score] = val; const ind = acc.findIndex(el => el['Subject'] === val[0]); if(ind !== -1){ if(score > acc[ind]["Top"][0]["score"]){ acc[ind]["Top"] = [{ "name": name,"score": score }]; }else if(score === acc[ind]["Top"][0]["score"]){ acc[ind]["Top"].push({ "name": name,"score": score }); } }else{ acc.push({ "Subject": sub,"Top": [{"name": name, "score": score}] }); }; return acc; }, []); }; console.log(JSON.stringify(groupScore(arr), undefined, 4));
Output
The output in the console will be −
const arr = [ ['Math', 'John', 100], ['Math', 'Jake', 89], ['Math', 'Amy', 93], ['Science', 'Jake', 89], ['Science', 'John', 89], ['Science', 'Amy', 83], ['English', 'John', 82], ['English', 'Amy', 81], ['English', 'Jake', 72] ]; const groupScore = arr => { return arr.reduce((acc, val, index, array) => { const [sub, name, score] = val; const ind = acc.findIndex(el => el['Subject'] === val[0]); if(ind !== -1){ if(score > acc[ind]["Top"][0]["score"]){ acc[ind]["Top"] = [{ "name": name,"score": score }]; }else if(score === acc[ind]["Top"][0]["score"]){ acc[ind]["Top"].push({ "name": name,"score": score }); } }else{ acc.push({ "Subject": sub,"Top": [{"name": name, "score": score}] }); }; return acc; }, []); }; console.log(JSON.stringify(groupScore(arr), undefined, 4));[ { "Subject": "Math", "Top": [ { "name": "John","score": 100 } ] }, { "Subject": "Science", "Top": [ { "name": "Jake", "score": 89 }, { "name": "John", "score": 89 } ] }, { "Subject": "English", "Top": [ { "name": "John", "score": 82 } ] } ]
Advertisements