-4
任何人都可以幫我用JavaScript函數來推翻這個下面的數組。這個結果我從數據庫中獲得,並打算使用JavaScript來做一些樹。如何推翻從兒童父母的數組結果樹到父母的孩子在Javascript中
[{
SubCategoryId: 1,
SubCategoryName: "SubCategoryName1",
Category: {
CategoryId: 11,
CategoryName: "CategoryName1",
MainCategory: {
MainCategoryId: 111,
MainCategoryName: "MainCategoryName1",
}
}
},{
SubCategoryId: 2,
SubCategoryName: "SubCategoryName2",
Category: {
CategoryId: 11,
CategoryName: "CategoryName1",
MainCategory: {
MainCategoryId: 111,
MainCategoryName: "MainCategoryName1",
}
}
},{
SubCategoryId: 3,
SubCategoryName: "SubCategoryName3",
Category: {
CategoryId: 22,
CategoryName: "CategoryName2",
MainCategory: {
MainCategoryId: 111,
MainCategoryName: "MainCategoryName1",
}
}
}]
我預期的結果是:
[{
MainCategoryId: 111,
MainCategoryName: "MainCategoryName1",
Category: [{
CategoryId: 11,
CategoryName: "CategoryName1",
SubCategory: [{
SubCategoryId: 1,
SubCategoryName: "SubCategoryName1",
}, {
SubCategoryId: 2,
SubCategoryName: "SubCategoryName2",
}]
}, {
CategoryId: 22,
CategoryName: "CategoryName2",
SubCategory: [{
SubCategoryId: 3,
SubCategoryName: "SubCategoryName3",
}]
}],
}]
感謝您的幫助
有什麼問題嗎? – m02ph3u5
結構來自孩子父母,我需要將其改爲父母子女 –