我有这样的数据:
export const keyOrders: {} = {
"aa": { active: true, order: 0, val: "aaa" },
"bb": { active: true, order: 6, val: "bbb" },
"cc": { active: true, order: 2, val: "ccc" },
"dd": { active: true, order: 7, val: "ddd" },
"ee": { active: false, order: 4, val: "eee" },
"ff": { active: true, order: 5, val: "fff" }
};
如果active仅是true,我将尝试推送至数组并根据订单值进行排序。
到目前为止,我已经尝试过了
this.pdfKeys = Object.entries(this.dashboardElementsConfig)
.filter(([key, value]) => {
return value["active"];
})
.map(([key, value]) => {
return key;
})
我不确定如何根据给出的订单号进行排序。
答案 0 :(得分:5)
您可以按照以下步骤进行操作。
Object.keys()
filter()
来
active:true
sort()
对过滤后的数组进行升序或降序排序。map()
将每个key
转换为值的.val
您可以首先使用所有active:true
个对象,然后分别使用map()
和key
达到所需的值
let obj = {
"aa": { active: true, order: 0, val: "aaa" },
"bb": { active: true, order: 6, val: "bbb" },
"cc": { active: true, order: 2, val: "ccc" },
"dd": { active: true, order: 7, val: "ddd" },
"ee": { active: false, order: 4, val: "eee" },
"ff": { active: true, order: 5, val: "fff" }
};
let asc = Object.keys(obj)
.filter(x => obj[x].active)
.sort((a,b) => obj[a].order - obj[b].order)
.map(x => obj[x].val);
let dec = Object.keys(obj)
.filter(x => obj[x].active)
.sort((a,b) => obj[b].order - obj[a].order)
.map(x => obj[x].val);
console.log(asc)
console.log(dec)