我希望基于用户的分数百分比显示所有用户的详细信息并为他们分配排名。这里我已经获取了所有的详细信息。如何排序和打印用户,请看下方代码:
同样,students对象中的每个用户也是如此。
Tom has scored marks in Science - 80 Maths - 89 English - 91 and secured 86.67 % and his rank is 1.
同样,students对象中的每个用户也是如此。
var students = [{
name: "John",
age: 20,
id: 1,
marks: {
science: 80,
maths: 90,
english: 67
}
}, {
name: "Jack",
age: 22,
id: 3,
marks: {
science: 56,
maths: 91,
english: 81
}
}, {
name: "Robert",
age: 23,
id: 2,
marks: {
science: 75,
maths: 79,
english: 87
}
}, {
name: "Tom",
age: 20,
id: 4,
marks: {
science: 80,
maths: 89,
english: 91
}
}];
var inDetail = getPercentageAndRank(students);
function getPercentageAndRank(students) {
var details = students.map(function(getMarks) {
console.log(((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3));
return {
name: getMarks.name,
science: getMarks.marks.science,
maths: getMarks.marks.maths,
english: getMarks.marks.english,
percentage: ((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3)
};
})
return details.map(function(gg) {
return [
gg.name,
"has scored marks in Science -",
gg.science,
"Maths -",
gg.maths,
"English -",
gg.english,
"and secured", (gg.percentage).toFixed(2),
"%."
].join(' ');
});
}
输出:
["John has scored marks in Science - 80 Maths - 90 English - 67 and secured 79.00 %.",
"Jack has scored marks in Science - 56 Maths - 91 English - 81 and secured 76.00 %.",
"Robert has scored marks in Science - 75 Maths - 79 English - 87 and secured 80.33 %.",
"Tom has scored marks in Science - 80 Maths - 89 English - 91 and secured 86.67 %."]
我得到的输出结果与之前所说的不符。如何获得上述结果。
请注意,这段话中的 HTML 标签已被保留。
reduce
,因为你之前类似问题的答案已经给出了 - http://stackoverflow.com/questions/32354201/how-to-combine-two-arrays-using-map-filter-reduce - 因为这次你想要的输出是一个数组...但你需要使用sort
。 - Jaromanda X