我有一个ibject数组,每个元素内都有另一个对象,例如
data = [
{
name: "A",
type: "AA",
children: [ { id: 1, name: "Child-A", admin: ["Y"] }],
other: "NA"
},
{
name: "B",
type: "BB",
children: [ { id: 2, name: "Child-B" }],
other: "NA"
},
{
name: "C",
type: "CC",
children: [ { id: 3, name: "Child-C" }],
other: "NA"
}
]
我想按children.id
值对整个集合进行排序,但要基于另一个数组给出的顺序
orderArray = [3, 1, 2]
所以输出将是
data =[
{
name: "C",
type: "CC",
children: [ { id: 3, name: "Child-C" }],
other: "NA"
},
{
name: "A",
type: "AA",
children: [ { id: 1, name: "Child-A", admin: ["Y"] }],
other: "NA"
},
{
name: "B",
type: "BB",
children: [ { id: 2, name: "Child-B" }],
other: "NA"
}
]
答案 0 :(得分:0)
答案 1 :(得分:0)
尝试一下:
var newdataArray = []
orderArray.forEach(index => {
newdataArray.push(data[index - 1])
});
data = newdataArray