I am having array of items with following order:
const arr = [
{
group: { id: "group1", groupname: "groupname1" },
categories: [
{
id: "cat1",
categoryName: "category1",
total: 5,
available: 2,
subCategories: []
},
{
id: "cat2",
categoryName: "category2",
total: 15,
available: 12,
subCategories: [
{
id: "cat3",
categoryName: "category3",
total: 15,
available: 12,
subCategories: []
}
]
}
]
},
{
group: { id: "group2", groupname: "groupname2" },
categories: [
{
id: "cat4",
categoryName: "category4",
total: 25,
available: 22,
subCategories: []
},
{
id: "cat5",
categoryName: "category5",
total: 50,
available: 25,
subCategories: []
}
]
}
];
This needs to be converted into array of recursive items, witj key, name, and children properties. The result would be like this,
[
{
"key": "group1",
"name": "groupname1",
"total": 20,
"available": 24,
"children": [
{
"key": "cat1",
"name": "category1",
"total": 5,
"available": 2,
"children": []
},
{
"key": "cat2",
"name": "category2",
"total": 15,
"available": 12,
"children": [
{
"key": "cat3",
"name": "category3",
"total": 15,
"available": 12,
"children": []
}
]
}
]
},
{
"key": "group2",
"name": "groupname2",
"total": 75,
"available": 47,
"children": [
{
"key": "cat4",
"name": "category4",
"total": 25,
"available": 22,
"children": []
},
{
"key": "cat5",
"name": "category5",
"total": 50,
"available": 25,
"children": []
}
]
}
]
I am able to bring down to the above structure using the following snippet.
const formatter = (data) => {
const recursiveTree = (item) => {
if (item.group) {
const {
group: { id, groupname, total, available },
categories
} = item;
return {
key: id,
name: groupname,
total: total || 0,
available: available || 0,
children: categories?.map(recursiveTree)
};
}
const { id, categoryName, total, available, subCategories } = item;
return {
key: id,
name: categoryName,
total: total || 0,
available: available || 0,
children: subCategories.map(recursiveTree)
};
};
return data.map(recursiveTree);
};
I am stuck at deriving total and available of the root note. This node should derive its total and available from its children values. Can someone help me how to do this recursively? This could be n level too
2
Answers
You could add some post-processing. Replace this:
with:
One approach separates out the reformatting of your input structure and the totaling of properties. Here we have an non-mutating version that works like that:
Here
restructure
converts your slightly odd input into a consistent recursive structure.sumOn
takes a field name and returns a function which totals values of that field by recursively adding those of each of itschildren
. (This would be slightly cleaner with asum
helper; but we can leave that as an exercise.)Our main function,
process
, simply callsrestructure
with the input, and then mapssumOn
over the results twice, once with'total'
and once with'available'
.I find this a very clean way to break down the problem into reusable chunks and simple steps.