I have an array of objects. You'll be able to find a sample of complete data here on JSONBlob.com.
Here's a simplified preview:
[
{
"name": "Unbetitelt",
"organizationMap": "60a55ed4e3a8973a02f910f1",
"childrenLayout": [{
"i": "60a64930cf1db1710b97bf7a",
}],
"id": "60a6472ecf1db1710b97bf4c"
},
{
"name": "middleparent",
"parentCubo": "60a6472ecf1db1710b97bf4c",
"organizationMap": "60a55ed4e3a8973a02f910f1",
"childrenLayout": [{
"i": "60a64936cf1db1710b97bf7d",
},
{
"i": "60a649afcf1db1710b97bfa6",
}
],
"id": "60a64930cf1db1710b97bf7a"
},
{
"name": "Unbetitelt",
"parentCubo": "60a64930cf1db1710b97bf7a",
"organizationMap": "60a55ed4e3a8973a02f910f1",
"childrenLayout": [
{
"i": "60a6494acf1db1710b97bf8f",
},
{
"i": "60a64976cf1db1710b97bf9a",
}
],
"id": "60a64936cf1db1710b97bf7d"
},
{
"name": "Unbetitelt",
"parentCubo": "60a64930cf1db1710b97bf7a",
"organizationMap": "60a55ed4e3a8973a02f910f1",
"childrenLayout": [],
"id": "60a649afcf1db1710b97bfa6"
},
{
"name": "Unbetitelt",
"parentCubo": "60a649c5cf1db1710b97bfb1",
"organizationMap": "60a55ed4e3a8973a02f910f1",
"childrenLayout": [],
"id": "60a649efcf1db1710b97bfc4"
}
]
Each object in this array(the root array) has an id and childrenLayout property.
The childrenLayout property is an array(the child array) of Objects which will have an i property but won't have another childrenLayout. So in order to find children of each item in the childrenLayout array, we'll have to use this i field and find objects in the root array.
Once we find those objects in the root array, if the childrenLayout property on those objects is not an empty array, then we have to repeat the same process and find objects in this object's childrenLayout, thereby creating a list of all ids that are linked to one specific object with id x.
So, for instance, if we try to find all the items linked with id 60a6472ecf1db1710b97bf4c, we'll get:
60a64930cf1db1710b97bf7a60a64936cf1db1710b97bf7d60a649afcf1db1710b97bfa660a6494acf1db1710b97bf8f60a64976cf1db1710b97bf9a
This is what I have so far and it's obviously not working as expcted and only returns the last three items from the expected items list mentioned above:
const findChildFor = item => {
if (item.childrenLayout.length > 0) {
const detailsOfItems = item.childrenLayout.map(({ i }) =>
data.find(datum => datum.id === i)
);
return detailsOfItems.map(itemDetails => findChildFor(itemDetails));
} else {
return item.id;
}
};
const [firstItem] = data;
// TODO: Work on a way to make it return children without having to flatten it.
const childrenRelatedToFirstItem = findChildFor(firstItem);
console.log(
'childrenRelatedToFirstItem: ',
childrenRelatedToFirstItem.flat(10)
);
To help speed up the solution, here's a Sample Stackblitz with the code so far.
Any help is greatly appreciated.