将等级从最高到最低等级作为对象值

时间:2018-05-29 02:05:51

标签: javascript arrays sorting object rank

我想从每个对象获取名字和gwa,然后根据他们的gwa从最高到最低排列。我得到了“获得名字和gwa”部分,但我不知道如何根据他们的gwa安排他们。

我尝试使用sort()但我似乎以错误的方式使用它。有人可以帮帮我吗?

const students = [
{
    firstName: "Jose",
    lastName: "Valenci",
    gender: "male",
    gwa: 98, 
    interest: "tennis"
},

{
    firstName: "Debby",
    lastName: "Smith",
    gender: "female",
    gwa: 96,
    interest: "singing"
},

{
    firstName: "Zaira",
    lastName: "Pay",
    gender: "female",
    gwa: 93,
    interest: "crafts"
}];


const basedOnGwa = students.map ((name,grade) => {
let{lastName} = name;
let {gwa} = grade;
grade = grade.sort(grade1 => {
    let {gwa} = grade1;
    return grade1;
}); 

return grade();
})

console.log(basedOnGwa);

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

ProjectName_Buildname

修改

如果您想使用const students = [ { firstName: "Jose", lastName: "Valenci", gender: "male", gwa: 98, interest: "tennis" }, { firstName: "Debby", lastName: "Smith", gender: "female", gwa: 96, interest: "singing" }, { firstName: "Zaira", lastName: "Pay", gender: "female", gwa: 93, interest: "crafts" }]; var basedOnGwa = students.reduce((arr,v,i)=>{ return arr.concat({'firstName':v.firstName,'gwa':v.gwa}); },[]); basedOnGwa.sort((a,b)=>{ return b.gwa - a.gwa; }) console.log(basedOnGwa);代替map,可以按照以下代码进行操作:

reduce