我有一个JSON数据表。但是之后,我想向谁展示谁是第一名。我尝试了很多次,但没有得到。 PFB ..
Noisyimage
to test
Tesseract OCR
Line text is: Noisyimageto testTesseract OCR
Extracted Text is: Noisyimageto testTesseract OCR
Data already exists
答案 0 :(得分:1)
简单地遍历数组并跟踪当前最大值。
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]);
答案 1 :(得分:0)
您可以使用array reducer来获得总排名最高的项目:
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" }
];
bestStudent = studentsList.reduce((acc, curr) => {
if (curr.sub1 + curr.sub2 + curr.sub3 > acc.sub1 + acc.sub2 + acc.sub3){
acc = curr
}
return acc
});
console.log(bestStudent)
答案 2 :(得分:0)
所以从问题上来说
要做的事情
解决方案说明
let 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 sortedArrayBasedOnTotal = studentsList.map(o => ({...o, total: Number(o.sub1) + Number(o.sub2) + Number(o.sub3)})).sort((a,b) => b.total - a.total)
console.log(sortedArrayBasedOnTotal)
排序后的数组可以解析为表格并显示在用户界面上
您可以通过访问
来访问第一名。 console.log(sortedArrayBasedOnTotal[0])
答案 3 :(得分:0)
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" }
];
// Added a new key 'total' for each student
// total = marks of subj1 + subj2 + subj3
// parseInt(string) -> For converting string to integer for mathematical operation
studentsList.forEach(function(part, index, studentsList) {
studentsList[index]['total'] = parseInt(studentsList[index]['sub1']) + parseInt(studentsList[index]['sub2']) + parseInt(studentsList[index]['sub3']);
});
// Here is the sorting of students on basis of 'total'
studentsList.sort((a,b) => (a.total > b.total) ? 1 : ((b.total > a.total) ? -1 : 0));
// Last student will have the first rank
// studentsList.length - 1 = last student in the sorted list
console.log('First rank student detail')
console.log(studentsList[studentsList.length - 1])
// All students
console.log('All students details')
console.log(studentsList)