【发布时间】:2019-09-30 00:21:26
【问题描述】:
我有一个对象数组:
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
}
]
}
]
我想按特定顺序排序:
entity.name:按字母升序排列。entity.entityType: 1. 个人 2. 商务 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 的方法?
【问题讨论】:
-
limit只是为了对facilities数组进行排序,还是你希望它也会影响到外部数组entities? -
只是设施数组。
标签: javascript arrays reactjs sorting