const packages = [{ id: '641a1690-6c8b-4ada-ae97-8d82cc4fe7a3', name: 'com.sample', children: { id: 'd7384f60-e4ab-4a86-8e2e-0f66cc32f', name: 'child.computer.com', children: { id: 'e4ab-4a86-0f66cc32f560', name: 'child.com' }}}, { id: 'd7384f60-e4ab-4a86-8e2e-0f66cc32f560', name: 'computer.com' }, { id: 'ca7f972e-64ee-4cb0-80b9-1036fac69d32', name: 'java.util' }];
const replacer = { "id": "key", "name" :"title"};
const transformObj = (obj) => {
if(obj && Object.getPrototypeOf(obj) === Object.prototype){
return Object.fromEntries(
Object.entries(obj)
.map(([k, v]) => [replacer[k] || k, transformObj(v)])
);
}
return obj;
}
console.log(packages.map(o => transformObj(o)));
TreeDataSource(arr){
if(!Array.isArray(arr)){return;}
return arr.map((v,i)=>{
if(v.children){
return {
...v,
id:v.agencyCode,
label: v.agencyCode + v.agencyName,
customLabel: v.agencyName,
children:this.TreeDataSource(v.children)
}
}else{
return {
...v,
id:v.agencyCode,
label: v.agencyCode + v.agencyName,
customLabel: v.agencyName
}
}
})
},