I want to set the active element to false in all of the objects. This is my object:
const obj =
{ name: 'obj1'
, ative: true
, children:
[ { name: 'obj2'
, ative: true
, children:
[ { name: 'Obj23'
, ative: true
, children: [...]
} ] }
, { name: 'obj3'
, children:
[ { name: 'Obj32'
, ative: true
, children: [...]
} ] }
, ...
}
what I have to is, for the main object obj1
and all of the childrens, and sub childres.
I want to set the active
to false
.
The point is, I don’t know how many childrens there can be under each child. I would have to create some type of loop with a map
or something of the kind.
I will need to stop looping when the children is equal to 0 (no length)
3
Answers
There are two main approaches that can be used
In most cases, the iterative approach is better in both memory and speed since the recursive approach requires
O(n)
extra memory for each invocation in the call stack.In the iterative approach
map
, andforEach
array methods will be useless, since we don’t know the length and when to stop, even if manage to use them it will require us to mutate the traversed array which leads to side-effects. Instead, we are going to use a simplewhile
loop.Algorithm:
active
tofalse
Implementation:
First, let’s create a type for the input data structure:
Now, the function itself:
playground
You can also use
JSON.parse
withreviver function
to do this:I don’t disagree with wonderflame that a stack-based approach is better if you don’t know how large your tree will grow, but if you know you will only ever go a few levels deep then the optimization might not be needed, and a recursive approach is simpler.