JavaScript R- eturn Array Item(s) With Largest Score

JavascriptWeb DevelopmentObject Oriented Programming

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
            }
      ]
   }
]
raja
Updated on 31-Aug-2020 12:36:17

Advertisements