I have a payload structure to add datas into db, each object have name with child array, db should add each object with parent id. how to handle these array in loop adding data in database?
payload structure:
{
"name": "food",
"child_categories": [
{
"name": "non-frozen",
"child_categories": [
{
"name":"non-branded",
"child_categories": [
{
"name":"food-grains",
"child_categories": [
{
"name":"rice"
},
{
"name": "atta"
},
{
"name": "maida"
},
{
"name": "sooji"
},
{
"name":"ragi"
},
{
"name": "jower"
},
{
"name": "bajra"
}
]
},
{
"name":"salt&spices",
"child_categories": [
{
"name":"whole-spices"
},
{
"name":"grounded-spices"
}
]
},
{
"name":"dried-condiments",
"child_categories": [
{
"name":"salt"
},
{
"name":"powders"
},
{
"name":"sugar"
},
{
"name":"jaggery"
}
]
},
{
"name": "edible-oil&ghee"
},
{
"name": "lentils",
"child_categories":[
{
"name":"all-dals"
},
{
"name":"all-rajma"
},
{
"name":"chanas"
},
{
"name":"millets"
},
{
"name":"soya"
}
]
}
]
},
{
"name":"branded",
"child_categories": [
{
"name":"food-grains",
"child_categories": [
{
"name":"rice"
},
{
"name": "atta"
},
{
"name": "maida"
},
{
"name": "sooji"
},
{
"name":"ragi"
},
{
"name": "jower"
},
{
"name": "bajra"
}
]
},
{
"name":"salt&spices",
"child_categories": [
{
"name":"whole-spices"
},
{
"name":"grounded-spices"
}
]
},
{
"name":"dried-condiments",
"child_categories": [
{
"name":"salt"
},
{
"name":"powders"
},
{
"name":"sugar"
},
{
"name":"jaggery"
}
]
},
{
"name": "edible-oil&ghee"
},
{
"name": "lentils",
"child_categories":[
{
"name":"all-dals"
},
{
"name":"all-rajma"
},
{
"name":"chanas"
},
{
"name":"millets"
},
{
"name":"soya"
}
]
},
{
"name":"snacks&cereals",
"child_categories":[
{
"name":"biscuits"
},
{
"name":"cookies"
},
{
"name":"bhujia"
},
{
"name":"mixture"
}
]
},
{
"name":"packaged-raw",
"child_categories":[
{
"name":"noodles"
},
{
"name":"pasta"
},
{
"name":"macaroni"
},
{
"name":"spreads"
},
{
"name":"sauces"
},
{
"name":"ready-to-eat"
},
{
"name":"chocs"
},
{
"name":"pickles&chutney"
}
]
}
]
}
]
}
]
}
Each categories have sub categories,with up level parent_id, once first level category added, it should take that id, add it into child category. example structure below
{
_id: "1",
parent_category_id: null, // 1st level category
name:"food"
}
{
_id:2,
parent_category_id: 1,
name: "non-frozen"
}
{
_id: 3,
parent_category_id: 2,
name: "non-branded"
}
{
_id: 4,
parent_category_id: 3,
name: "food-grains"
}
and so on...
let payload = req.payload
let obj1 = {
"name": payload.name,
"parent_category_id": null,
"created_by": userId,
"updated_by": userId
}
let parent_category = await Category.create(obj1)
if (!!payload.childCategories){
payload.childCategories.forEach(async item => {
if(!!item){
let obj2 = {
"name": item.name,
"parent_category_id": parent_category._id,
"created_by": userId,
"updated_by": userId
}
let parent_category1 = await Category.create(obj2)
if (!!item.childCategories){
payload.childCategories.forEach(async item1 => {
let obj3 = {
"name": item1.name,
"parent_category_id": parent_category1._id,
"created_by": userId,
"updated_by": userId
}
let parent_category2 = await Category.create(obj3)
})
}
}
})
}
CodePudding user response:
My quick thought was to solve the problem with the recursive function. It can be solved in other ways as well. But I like recursive functions. They are legible.
async function addCategory(payload, parent = null) {
const category = await Category.create({
name: payload.name,
parent_category_id: parent ? parent.id : null,
created_by: userId,
updated_by: userId
});
if(Array.isArray(payload.child_categories)) {
for(const child of payload.child_categories) {
addCategory(child, category);
}
}
}