在表格列表显示之后,谁是第一名

时间:2019-10-30 07:38:27

标签: javascript json

我有一个JSON数据表。但是之后,我想向谁展示谁是第一名。我尝试了很多次,但没有得到。 PFB ..

Noisyimage
to test
Tesseract OCR
Line text is: Noisyimageto testTesseract OCR
Extracted Text is: Noisyimageto testTesseract OCR
Data already exists

4 个答案:

答案 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)

所以从问题上来说

要做的事情

  1. 需要总结三个属性sub1,sub2,sub3。
  2. 根据三个属性的总值对数组进行排序。

解决方案说明

  1. 创建一个新数组(array.map)而不是变异同一数组
  2. 在新数组上,创建一个名为total的属性,该属性将汇总三个属性值,并通过动态更改类型将格式更改为number。
  3. 使用内置的sort函数,它将对数组副本(array.sort)进行排序

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)

  • 第1步:计算所有学生的所有学科分数总计
  • 第2步:根据总分对学生进行排序
  • 第3步:根据需要获取排名

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)