gpt4 book ai didi

javascript - 如何在 javascript 中对嵌套的对象数组进行排序?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:53:22 24 4
gpt4 key购买 nike

我有一个对象数组:

entities: [
{
name: "zBroomsticks PTY",
id: 34098365,
entityType: "personal",
facilities: [
{
type: "Home loan",
account: "032654 987465",
existing: true,
modified: "04/12/2018",
limit: 100000
},
{
type: "Credt card",
account: "032654 987465",
existing: false,
modified: "04/12/2018",
limit: 200000
},
{
type: "Credt card",
account: "032654 987465",
existing: false,
modified: "04/12/2018",
limit: 10000
},
{
type: "Credt card",
account: "032654 987465",
existing: false,
modified: "04/12/2018",
limit: 10000
}
]
},
{
name: "Mr John Doe -3409865, Mrs Jane Doe -34098365",
id: 34098365,
entityType: "business",
facilities: [
{
type: "Overdraft",
account: "032654 987465",
existing: false,
modified: "04/12/2018" ,
limit: 10000
}
]
},
{
name: "Mr Jack",
id: 34098365,
entityType: "mixed",
facilities: [
{
type: "Overdraft",
account: "032654 987465",
existing: false,
modified: "04/12/2018",
limit: 10000
}
]
}
]

我想按特定顺序对其进行排序:

  1. entity.name:按字母升序排列。

  2. entity.entityType: 1.个人 2.企业 3.混合

  3. entity.facilities.limit:降序

这是我目前得到的代码:

sortData(entities) {
var order = {
entityType: { personal: 2, business: 1 }
};

return entities.map(ent =>
entities.sort(
(a, b) =>
a.name - b.name ||
order.entityType[a.facilities.entityType] - order.entityType[b.facilities.entityType]
)
);
}

我知道如何执行名称排序但找不到 2 和 3 的方法?

Link to code

最佳答案

首先,要按 names 排序,您可以使用 localeCompare() .其次,facilities 数组中没有属性 entityType 但您正在尝试访问它。现在,一种解决方案是首先使用 Array.map()获取一个新数组,其中 facilities 数组按 limit 属性排序,然后您可以先对 map() 返回的新数组进行排序通过 names 然后通过 entityType 属性,如下所示:

const input = [{name:"zBroomsticks PTY",id:34098365,entityType:"personal",facilities:[{type:"Home loan",account:"032654 987465",existing:true,modified:"04/12/2018",limit:100000},{type:"Credt card",account:"032654 987465",existing:false,modified:"04/12/2018",limit:200000},{type:"Credt card",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000},{type:"Credt card",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]},{name:"Mr John Doe -3409865, Mrs Jane Doe -34098365",id:34098365,entityType:"business",facilities:[{type:"Overdraft",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]},{name:"Mr Jack",id:34098365,entityType:"mixed",facilities:[{type:"Overdraft",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]},{name:"Mr Jack",id:34098365,entityType:"personal",facilities:[{type:"Overdraft",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]}];

let order = {
entityType: {personal:1, business:2, mixed:3}
};

function sortData(entities)
{
let limitOrder = entities.map(e =>
{
e.facilities.sort((a, b) => b.limit - a.limit);
return e;
});

return limitOrder.sort((a, b) =>
{
return a.name.localeCompare(b.name) ||
order.entityType[a.entityType] - order.entityType[b.entityType];
});
}

console.log(sortData(input));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

请注意,我使用不同的 entityType 复制了与 name: "Mr Jack" 相关的对象,因此您可以看到当有两个对象时算法的执行情况等于 names

关于javascript - 如何在 javascript 中对嵌套的对象数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56105584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com