I have one nested JavaScript object and I want to add new key value in each nested object. So basically I have to create custom function to adding it.
**My Input Object:
**
const obj = {
name: {
final: {
val: 'yes'
}
}
}
**Expected Object:
**
const outObj = {
id: 1,
name: {
id: 1,
final: {
id: 1,
val: 'yes'
}
}
}
2
Answers
You can recursive function and reduce to get the output:
The solution is basic recursion. You figure out if any properties of the object is an object and you loop over that one and so on.