var json
= [
{
"id" : "01",
"pId":""
} ,
{
"id" : "01_01",
"pId":"01"
} ,
{
"id" : "01_02",
"pId":"01"
} ,
{
"id" : "01_02_01",
"pId":"01_02"
} ,
{
"id" : "02",
"pId":""
},
{
"id" : "02-01",
"pId":"02"
},
{
"id" : "02-02",
"pId":"02"
},
{
"id" : "03",
"pId":""
},
{
"id" : "03_01",
"pId":"03"
}
];
function format(json
){
var ret
= [], o
= {};
function add(arr
, data
){
var obj
= {
"id": data
.id
,
"pId": data
.pId
,
"childer": []
};
o
[data
.id
] = obj
;
arr
.push(obj
);
}
json
.forEach(x
=> {
if(o
[x
.pId
]){
add(o
[x
.pId
].childer
, x
);
}else{
add(ret
, x
);
}
});
return ret
;
}
输出
转载请注明原文地址:https://tech.qufami.com/read-5749.html