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)
EDIT: All solutions where really awesome. Thanks
You can also use JSON.parse
with reviver function
to do this:
JSON.parse(JSON.stringify(obj), (key, value ) => key === 'ative' ? false : value );
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: []
}]
}]
}
const result = JSON.parse(JSON.stringify(obj), (key, value) => key === 'ative' ? false : value);
console.log(result);