我有以下資料
{
"obj": ["2022","2022","2022","2023","2023","2023"],
"obj1": ["PPP","PPP","AAA","AAA","CCC","CCC"],
"obj2": [0,1,2,3,4,5]
}
資料中每個 index 互相對應,如"2022=>PPP=>0", "2022=>PPP=>1"...等. 資料階層是由key是obj -> obj1 -> obj2,有個陣列專門設定資料階層
var hierachyLevel = ["obj", "obj1", "obj2"]; //可動態變化,對應資料的key名稱
預期輸出:
categories: [
{
name: "2022",
categories: [
{
name: "PPP",
categories: [
"0",
"1"
]
},
{
name: "AAA",
categories: [
"2"
]
}
]
},/////////////////////
{
name: "2023",
categories: [
{
name: "AAA",
categories: [
"3"
]
},
{
name: "CCC",
categories: [
"4",
"5"
]
}
]
}
]
目前嘗試這個解法,以及自己寫無法解
如可幫忙非常感謝