代码之家  ›  专栏  ›  技术社区  ›  GAURAV VAGHELA

基于另一个自定义数组对数组中的对象属性排序[重复]

  •  0
  • GAURAV VAGHELA  · 技术社区  · 6 年前

    这个问题已经有了答案:

    我有一个对象数组

    item_array = [{
                    "Email Address": "c",
                    "First Name": "a",
                    "Last Name": "b",
                    "Permission": "Training Administrator",
                    "Telephone": "d",
                    "User Group": "Company Administrator"
                },
                {
                    "Email Address": "3",
                    "First Name": "1",
                    "Last Name": "2",
                    "Permission": "6",
                    "Telephone": "4",
                    "User Group": "5"
                }];
    

    如何按这样的给定数组对该数组进行排序

    item_order = ["First Name", "Last Name", "Email Address", "Permission", "Telephone", "User Group"];
    

    我需要像给定数组一样对对象数组进行排序

    依序

    预期结果

    item_array = [{
                    "First Name": "a",
                    "Last Name": "b",
                    "Email Address": "c",
                    "Permission": "Training Administrator",
                    "Telephone": "d",
                    "User Group": "Company Administrator"
                },
                {
                    "First Name": "1",
                    "Last Name": "2",
                    "Email Address": "3",
                    "Permission": "6",
                    "Telephone": "4",
                    "User Group": "5"
                }];
    
    2 回复  |  直到 6 年前
        1
  •  6
  •   adiga    6 年前

    你可以使用 reduce forEach 这样地:

    const item_array=[{"Email Address":"c","First Name":"a","Last Name":"b","Permission":"Training Administrator","Telephone":"d","User Group":"Company Administrator"},{"Email Address":"3","First Name":"1","Last Name":"2","Permission":"6","Telephone":"4","User Group":"5"}]
    
    ,item_order=["First Name","Last Name","Email Address","Permission","Telephone","User Group"]
    
    ,ordered = item_array.reduce((acc, a, i) => {
        acc[i] = {}, 
        item_order.forEach(o => acc[i][o] = a[o])
        return acc
      }, []);
    
    console.log(ordered)
        2
  •  1
  •   Fullstack Guy    6 年前

    以引用数组索引为排序顺序,在排序函数中实现排序逻辑,提取对象的键,按照给定的数组进行排序,并将排序后的对象推送到原始数组中。

    var item_array = [{ "Email Address": "c", "First Name": "a", "Last Name": "b", "Permission": "Training Administrator",  "Telephone": "d", "User Group": "Company Administrator" }, {"Email Address": "3", "First Name": "1", "Last Name": "2", "Permission": "6", "Telephone": "4", "User Group": "5"}];
    var item_order = ["First Name", "Last Name", "Email Address", "Permission", "Telephone", "User Group"];
    item_array.forEach((obj, idx, arr) => {
               arr[idx] = Object.keys(obj)
                                .sort((a, b) => {
                                 return item_order.indexOf(a) - item_order.indexOf(b);
                                 })
                                .reduce((acc, ele)=>{acc[ele] = obj[ele]; return acc;},{});
                      });
    
    console.log(item_array);