let obj = {
"rec": [{
"id": "25837",
"contentId": "25838"
},
{
"id": "25839",
"contentId": "25838"
},
{
"id": "25838"
},
{
"id": "25636",
"contentId": "25837"
}, {
"id": "25640",
"contentId": "25839"
}
]
};
I am working with a JavaScript object that requires manipulation to achieve the desired format.
{
"children": [{
"id": "25838",
"children": [{
"id": "25837",
"contentId": "25838",
"children": [{
"id": "25636",
"contentId": "25837"
}]
},
{
"id": "25839",
"contentId": "25838",
"children": [{
"id": "25640",
"contentId": "25839"
}]
}
]
}]
}
If an object does not have a contentId
, it should be at the parent level. Subsequently, objects with a contentId
matching the parent id should be placed at its child level and so on.
I have set up a demo in this fiddle here, but I need help completing the logic. Do you have any ideas or references to help me achieve this?