You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
929 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

Page({
data: {
students:[],
rankings: []
},
onLoad() {
const app = getApp();
let students = app.globalData.students || []; // 获取全局数据
// 根据 score 排序并取前 10 位
students = students.sort((a, b) => b.score - a.score).slice(0, 10);
// 添加 rank 属性
students.forEach((student, index) => {
student.rank = index + 1; // 设置排名,索引加 1
});
// 获取前 10 位中的后 7 位即4到10
const rankings = students.slice(3, 10); // 从索引 3 开始取到索引 10不包括10
this.setData({
students: students, // 设置前 10 位学生
rankings: rankings // 设置后 7 位学生
});
// console.log("前 10 位学生:", this.data.students); // 调试时打印前 10 位学生数据
// console.log("后 7 位学生:", this.data.rankings); // 调试时打印后 7 位学生数据
}
});