JavaScript 用獲取分數最高的陣列項


我們有一個數組,其中包含一些學生在某些科目中獲得的分數

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

我們需要編寫一個函式,該函式接受此陣列並返回一個物件陣列,其中每個物件對應一門科目,幷包含該科目中最高得分者的詳細資訊。

我們的輸出應如下所示

[
   { "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}
      ]
   }
]

我們編寫此函式的程式碼如下

示例

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

輸出

控制檯中的輸出如下

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

更新於: 31-8 月 -2020

112 次瀏覽

職業生涯起步

完成課程獲取認證

開始學習
廣告
© . All rights reserved.