// renameFields items->children
const renameFields = (obj)=> {
let newObj = {}
newObj.name = obj.title
newObj.url = obj.url
newObj.children = obj.items
return newObj
}
// transform Data
const transformData = (obj) => {
if(obj.items === null) {
obj = renameFields(obj)
obj.children = []
} else if(obj.items) {
obj.items = obj.items.map(item => transformData(item))
obj = renameFields(obj)
} else {
obj = renameFields(obj)
}
return obj
}
/** renameFields items->children **/
const renameFields = (obj) => ({
name: obj.title,
url: obj.url,
children: obj.items,
});
/** transform Data **/
const transformData = (data) => {
const renamedData = renameFields(data);
if(data.items) {
renamedData.children = renamedData.children.map((child) => renamedData(child));
return renamedData;
}
if(data.items === null) {
renamedData.children = [];
return renamedData;
}
return renamedData;
}