当前位置:网站首页>There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first

There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first

2022-07-26 03:10:00 Two faces confused

   subject : There are a group of students in the class whose exam results come out , I took Chinese and Mathematics , Please select the students whose total score is the first

requirement :
  1. Data types are defined by themselves , It can be screened out
  2. With the same total score , It's tied for the first place They're all screened out

const arr = [
    {id: 1, name: ' Xiao Zhang ', language: '98', math: '100'},
    {id: 2, name: ' petty thief ', language: '90', math: '98'},
    {id: 3, name: ' Zhang San ', language: '98', math: '100'},
    {id: 4, name: ' Li Si ', language: '90', math: '97.5'},
    {id: 5, name: ' Wang Wu ', language: '99', math: '99'},
    {id: 6, name: ' Zhao Liu ', language: '78', math: '88'}
];

let topArr = [];
for (let i = 1; i < arr.length; i++) {
    if ((arr[i].language + arr[i].math) > (arr[i - 1].language + arr[i - 1].math)) {
        topArr.push(arr[i])
    } else if ((arr[i].language + arr[i].math) == (arr[i - 1].language + arr[i - 1].math)) {
        topArr.push(arr[i], arr[i - 1])
    } else {
        topArr.push(arr[i - 1])
    }
}

console.log(new Set(topArr));// adopt new Set() Deduplication 

 Insert picture description here

link : Array de duplication method

原网站

版权声明
本文为[Two faces confused]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207260306476056.html