【问题标题】:Is it possible to sort an array of object by property of another object?是否可以通过另一个对象的属性对对象数组进行排序?
【发布时间】:2020-08-29 09:09:11
【问题描述】:

所以我有一个如下所示的对象数组:

    group: [
0: {id: "16", name: "P1", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 1, 1: 10, 2: 11},…}
1: {id: "17", name: "C1", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 15, 1: 16, 2: 18},…}
2: {id: "22", name: "P2", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 12, 1: 13, 2: 9},…}
3: {id: "23", name: "C2", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 17, 1: 19, 2: 20},…}
4: {id: "24", name: "DEV", courseId: "10", mentorId: "1", chatUrl: false,…}]. 

我正在尝试按导师姓名对其进行排序,导师姓名是另一个数组中对象的属性,如下所示:

mentor: [
0: {id: "0", firstName: "Daniel",  about: false,…}
1: {id: "1", firstName: "Mark",  aboutl:false,…}
2: {id: "3", firstName: "Eric", about: false,…}
3: {id: "6", firstName: "John", about: false,…} ]

这些组通过属性mentorId 与导师相关,因此我必须将组数组中的属性mentorId 与导师数组中的id 进行比较,以获取组的所有导师,然后按这些导师的名字对组进行排序。这甚至可能吗?

输出应显示按导师名字字母顺序排序的所有组,如下所示:

group: [
0: {id: "23", name: "C2", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 17, 1: 19, 2: 20},…}
    1: {id: "17", name: "C1", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 15, 1: 16, 2: 18},…}
    2: {id: "22", name: "P2", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 12, 1: 13, 2: 9},…}
    3: {id: "16", name: "P1", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 1, 1: 10, 2: 11},…}
    4: {id: "24", name: "DEV", courseId: "10", mentorId: "1", chatUrl: false,…}
]

【问题讨论】:

  • 输出会是什么样子?
  • “按导师姓名排序”是什么意思?按升序排序?还是按照导师姓名在导师数组中出现的顺序排序?
  • 我编辑了这个问题,它应该按导师名字的字母顺序对组进行排序,升序或降序,这并不重要

标签: javascript arrays sorting object


【解决方案1】:

您可以首先按 firstName 属性对导师数组进行排序,然后创建一个对象,其中键是 id,值是该对象在排序数组中的索引。然后你可以在 groups 数组上使用 sort 方法,并在 order 对象中按 mentorId 属性的值排序。

const groups = [{"id":"16","name":"P1","courseId":"6","mentorId":"1","chatUrl":false,"students":{"0":1,"1":10,"2":11}},{"id":"17","name":"C1","courseId":"7","mentorId":"3","chatUrl":false,"students":{"0":15,"1":16,"2":18}},{"id":"22","name":"P2","courseId":"6","mentorId":"1","chatUrl":false,"students":{"0":12,"1":13,"2":9}},{"id":"23","name":"C2","courseId":"7","mentorId":"3","chatUrl":false,"students":{"0":17,"1":19,"2":20}},{"id":"24","name":"DEV","courseId":"10","mentorId":"1","chatUrl":false}]
const mentors = [{"id":"0","firstName":"Daniel","about":false},{"id":"1","firstName":"Mark","aboutl":false},{"id":"3","firstName":"Eric","about":false},{"id":"6","firstName":"John","about":false}]

mentors.sort((a, b) => a.firstName.localeCompare(b.firstName))
const order = mentors.reduce((r, { id }, i) => (r[id] = i, r), {})
groups.sort((a, b) => order[a.mentorId] - order[b.mentorId])
console.log(groups)

【讨论】:

    【解决方案2】:
    var group= [
         
         {id: "17", name: "C1", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 15, 1: 16, 2: 18}},
         {id: "16", name: "P1", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 1, 1: 10, 2: 11}},
         {id: "22", name: "P2", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 12, 1: 13, 2: 9}},
         {id: "23", name: "C2", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 17, 1: 19, 2: 20}},
         {id: "24", name: "DEV", courseId: "10", mentorId: "1", chatUrl: false}] 
    
    var mentor=[
        {id: "0", firstName: "Daniel",  about: false},
        {id: "1", firstName: "Mark",  aboutl:false},
        {id: "3", firstName: "Eric", about: false},
        {id: "6", firstName: "John", about: false} 
    ]
    
    group = group.sort( (a,b) => {
        for(i=0;i<mentor.length;i++){
            if (a.mentorId===mentor[i].id){a.mentorName=mentor[i].firstName}
            if (b.mentorId===mentor[i].id){b.mentorName=mentor[i].firstName}
            
        }
        if(a.mentorName<b.mentorName){return -1} 
        else if(a.mentorName==b.mentorName){return 0} 
        else{return 1}
    });
    console.log(group);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2023-01-06
      相关资源
      最近更新 更多