const array= [
{
"groupId": "group1",
"devices": ["5","6"],
"children": [
{
"groupId": "group11",
"devices": ["1","2","3","4"],
"children": [
{
"groupId": "group111",
"devices": ["7","8","9","0"],
"children": []
},
{
"groupId": "group112",
"devices": ["7","8","9","0"],
"children": []
}
]
},
{
"groupId": "group12",
"devices": ["1","2","3","4"],
"children": [
{
"groupId": "group121",
"devices": ["7","8","9","0"],
"children": []
}
]
}
]
}
]
This is an array which could have n number of children nested.
The expected outcome with a recursive typescript function should return the count of all devices under a group.
For example:
group1 : 22
group11 : 12
group11 : 8
group121 : 4
I have created a method which would generate the count and add to the array which is not generating the desired results.
this.getdevicesCount(this.array, 0);
/**
* Recursive method to find Number of devices in a group
*/
getdevicesCount(array: any[], devcount: number) {
array.forEach((group) => {
devcount = devcount + group.devices.length;
if(group.children.length) {
this.getdevicesCount(group.children, devcount);
}
group.count = devcount;
devcount = 0;
});
}
2
Answers
The issue is that in JS/TS, arguments are passed by value. In particular, your 2nd parameter
devcount
is a primitive type (number
), so there is no way modifying its value within the body ofthis.getdevicesCount
function could have the side effect of modifying the argument value outside.Instead of trying to modify the argument value, you could just
return
it:Your function only computes a value for a single key, if you want all counts in an object, you’ll need a function that returns the count and populates the object at the same time, for example: