gpt4 book ai didi

javascript - 表格列表后显示谁是第一名的持有者

转载 作者:行者123 更新时间:2023-12-02 22:39:49 25 4
gpt4 key购买 nike

我有一个 JSON 数据表。但在那之后,我想告诉它谁是第一名。我尝试了很多次但我没有得到它。PFB..

var studentsList = [
{ "id": "1", "firstName": "rajesh", "lastName": "kumar", "sub1": "35", "sub2": "55", "sub3": "45" },
{ "id": "2", "firstName": "ranjith", "lastName": "rajesh", "sub1": "56", "sub2": "65", "sub3": "44" },
{ "id": "3", "firstName": "arun", "lastName": "LK", "sub1": "48", "sub2": "89", "sub3": "88" },
{ "id": "4", "firstName": "abinaya", "lastName": "Vishwa", "sub1": "65", "sub2": "67", "sub3": "65" },
{ "id": "5", "firstName": "ashok", "lastName": "kumar", "sub1": "33", "sub2": "44", "sub3": "100" },
{ "id": "6", "firstName": "ashwini", "lastName": "kumar", "sub1": "89", "sub2": "35", "sub3": "100" },
{ "id": "7", "firstName": "karthick", "lastName": "kanagaraj", "sub1": "90", "sub2": "89", "sub3": "63" },
{ "id": "8", "firstName": "saravanan", "lastName": "fransis", "sub1": "84", "sub2": "81", "sub3": "83" },
{ "id": "9", "firstName": "Antoty", "lastName": "john", "sub1": "55", "sub2": "100", "sub3": "65" },
{ "id": "10", "firstName": "santhosh", "lastName": "Arun", "sub1": "33", "sub2": "100", "sub3": "83" }
];

最佳答案

只需迭代数组并跟踪当前最大值。

const studentsList = [
{ "id": "1", "firstName": "rajesh", "lastName": "kumar", "sub1": "35", "sub2": "55", "sub3": "45" },
{ "id": "2", "firstName": "ranjith", "lastName": "rajesh", "sub1": "56", "sub2": "65", "sub3": "44" },
{ "id": "3", "firstName": "arun", "lastName": "LK", "sub1": "48", "sub2": "89", "sub3": "88" },
{ "id": "4", "firstName": "abinaya", "lastName": "Vishwa", "sub1": "65", "sub2": "67", "sub3": "65" },
{ "id": "5", "firstName": "ashok", "lastName": "kumar", "sub1": "33", "sub2": "44", "sub3": "100" },
{ "id": "6", "firstName": "ashwini", "lastName": "kumar", "sub1": "89", "sub2": "35", "sub3": "100" },
{ "id": "7", "firstName": "karthick", "lastName": "kanagaraj", "sub1": "90", "sub2": "89", "sub3": "63" },
{ "id": "8", "firstName": "saravanan", "lastName": "fransis", "sub1": "84", "sub2": "81", "sub3": "83" },
{ "id": "9", "firstName": "Antoty", "lastName": "john", "sub1": "55", "sub2": "100", "sub3": "65" },
{ "id": "10", "firstName": "santhosh", "lastName": "Arun", "sub1": "33", "sub2": "100", "sub3": "83" }
];

let highestMarks = 0,
highestMarksIndex = 0;

studentsList.reduce((acc, curr, index) => {
const totalMarks = curr.sub1 + curr.sub2 + curr.sub3;

if (totalMarks > highestMarks) {
highestMarks = totalMarks;
highestMarksIndex = index;
}
return acc;
}, {});

console.log('Highest marks student', studentsList[highestMarksIndex]);

关于javascript - 表格列表后显示谁是第一名的持有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58620538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com