I am trying to push the Id, typography and active and converting the nested one to flat JSON to use ReactDND npm library
I have JSON like this
[
{
"menuHead":[
{
"name":"MenuHead 01",
"description":"sample Head 01",
"icon":"sample01.png",
"products":[
{
"name":"product 01",
"description":"sample product ONE",
"icon":"product01.png",
"section":[
{
"name":"section01",
"description":"ActiveLayer",
"icon":"img.png",
"min":100,
"max":1000,
},
{
"name":"section02",
"description":"ActiveLayer",
"icon":"img.png",
"min":500,
"max":1000,
},
{
"name":"section03",
"description":"ActiveLayer",
"icon":"img.png",
"min":100,
"max":600,
}
]
}
]
}
]
}
]
I want push Id, typography and active as boolean to the array of objects and I want convert the JSON to Flat JSON as well (I want sections - typography to be same and Menu head, products typography to be different, similarly sections should have active as false and others as true)
I want output like this
[
{
"name":"MenuHead 01",
"description":"sample Head 01",
"icon":"sample01.png",
"id":1,
"typography":0,
"active":true,
},
{
"name":"product 01",
"description":"sample product ONE",
"icon":"product01.png",
"id":2,
"typography":1,
"active":true,
},
{
"name":"section01",
"description":"ActiveLayer",
"icon":"img.png",
"min":100,
"max":1000,
"id":4,
"typography":2,
"active":false,
},
{
"name":"section03",
"description":"ActiveLayer",
"icon":"img.png",
"min":100,
"max":600,
"id":5,
"typography":2,
"active":false,
},
{
"name":"section03",
"description":"ActiveLayer",
"icon":"img.png",
"min":100,
"max":600,
"id":6,
"typography":2,
"active":false,
}
]
I have tried this I am getting error and I want don't want to mention key example (obj.menuHead, obj.products, obj.sections)since the name will be changed later something like recursive way
const tree = [
{
"menuHead":[
{
"name":"MenuHead 01",
"description":"sample Head 01",
"icon":"sample01.png",
"products":[
{
"name":"product 01",
"description":"sample product ONE",
"icon":"product01.png",
"sections":[
{
"name":"section01",
"description":"ActiveLayer",
"icon":"img.png",
"min":100,
"max":1000,
},
{
"name":"section02",
"description":"ActiveLayer",
"icon":"img.png",
"min":500,
"max":1000,
},
{
"name":"section03",
"description":"ActiveLayer",
"icon":"img.png",
"min":100,
"max":600,
}
]
}
]
}
]
}
]
function addUniqueID(arr: any[], i: number) {
arr.forEach((obj) => {
obj.typography = i;
obj.id = i + 1;
obj.active = true;
if (obj.menuHead) {
addUniqueID(obj.menuHead, i + 1);
}
else if (obj.sections) {
addUniqueID(obj.sections, i + 1);
}
else if (obj.products) {
addUniqueID(obj.products, i + 1);
} else {
obj.active = false;
}
});
}
addUniqueID(tree, 0);
console.log(tree)
const c = (item: any) => "name" in item ? [{
name: item.name,
id: item.id,
typography: item.typography,
active: item.active,
description:item.description,
icon: item.icon,
min:item.min,
max:item.max
}] : []
function flat(item: any) {
if ("products" in item) {
return c(item).concat(item.products.flatMap(flat))
}
if ("sections" in item) {
return c(item).concat(item.sections.flatMap(flat))
}
return (
c(item)
)
}
console.log(JSON.stringify (tree.flatMap(flat)))