我正在嘗試對 Array 型別的資料進行遞回排序。
這是資料結構。
const DATA = [
{
id: 123,
name: "kevin",
children: [
{
id: 345,
name: "luke",
children: [
{
id: 67895,
name: "jane",
children: [{ id: 556, name: "che", children: [] }]
},
{
id: 89760,
name: "kendrick",
children: [
{ id: 4627, name: "auro", children: [] },
{ id: 777, name: "civil", children: [] },
{ id: 37654, name: "hobbit", children: [] }
]
}
]
},
{ id: 123215, name: "ron", children: [] }
]
},
{ id: 7642, name: "dobby", children: [] },
{ id: 2589, name: "porter", children: [] }
];
我想按“id”或“name”排序。
這是我嘗試過的。
const sorting = (array, label, sortedBy) => {
if (sortedBy === "asc")
return array.sort((a, b) => (a[label] > b[label] ? 1 : -1));
return array.sort((a, b) => (b[label] > a[label] ? 1 : -1));
};
const sortingData = (data, label, sort) => {
let result;
for (let i = 0; i < data.length; i ) {
result = sorting(data, label, sort);
if (data[i].children && data[i].children.length) {
result[i].children = sortingData(data[i].children, label, sort);
}
}
return result;
};
// Sample data
const DATA = [
{
id: 123,
name: "kevin",
children: [
{
id: 345,
name: "luke",
children: [
{
id: 67895,
name: "jane",
children: [{ id: 556, name: "che", children: [] }]
},
{
id: 89760,
name: "kendrick",
children: [
{ id: 4627, name: "auro", children: [] },
{ id: 777, name: "civil", children: [] },
{ id: 37654, name: "hobbit", children: [] }
]
}
]
},
{ id: 123215, name: "ron", children: [] }
]
},
{ id: 7642, name: "dobby", children: [] },
{ id: 2589, name: "porter", children: [] }
];
const data = sortingData(DATA, "id", "asc");
console.log(data);
在我糟糕的邏輯中,它似乎作業但不能正常作業。因為專案的孩子或孩子的孩子沒有排序。:(
我應該修復什么?或者我的方法完全錯誤?
非常感謝你的幫助。
以下是我想要的:
const DATA = [
{
id: 123,
name: "kevin",
children: [
{
id: 345,
name: "luke",
children: [
{
id: 67895,
name: "jane",
children: [{ id: 556, name: "che", children: [] }]
},
{
id: 89760,
name: "kendrick",
children: [
{ id: 777, name: "civil", children: [] },
{ id: 4627, name: "auro", children: [] },
{ id: 37654, name: "hobbit", children: [] }
]
}
]
},
{ id: 123215, name: "ron", children: [] }
]
},
{ id: 2589, name: "porter", children: [] },
{ id: 7642, name: "dobby", children: [] }
];
uj5u.com熱心網友回復:
基本上,您可以根據order傳遞給自定義函式的引數更改 a 和 b 排序引數的順序。然后還根據當前引數的型別(字串或數字)使用不同型別的排序方法。
const DATA = [{"id":123,"name":"kevin","children":[{"id":345,"name":"luke","children":[{"id":67895,"name":"jane","children":[{"id":556,"name":"che","children":[]}]},{"id":89760,"name":"kendrick","children":[{"id":4627,"name":"auro","children":[]},{"id":777,"name":"civil","children":[]},{"id":37654,"name":"hobbit","children":[]}]}]},{"id":123215,"name":"ron","children":[]}]},{"id":7642,"name":"dobby","children":[]},{"id":2589,"name":"porter","children":[]}]
function sortData(data, label, order) {
data.sort((a, b) => {
const x = order === 'asc' ? a : b;
const y = order === 'asc' ? b : a;
if ([x[label], y[label]].some(e => typeof e === 'string')) {
return x[label].localeCompare(y[label])
} else {
return x[label] - y[label]
}
})
data.forEach(el => {
if (el.children) {
sortData(el.children, label, order)
}
})
}
sortData(DATA, 'name', 'desc')
console.log(JSON.stringify(DATA, 0, 4))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/407407.html
標籤:
