Last active
February 28, 2018 03:46
-
-
Save thomashibbard/a4b74030e4115c0197b2167b02837e93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addPropToArrayOfObjectsRecursive = (prop, value, array, result = {}) => | |
array.reduce((accum, object, index) => | |
Object.hasOwnProperty.call(object, 'children') ? [ | |
...accum, | |
{ | |
...object, | |
[prop]: value, | |
children: addPropToArrayOfObjectsRecursive( | |
prop, value, object.children, accum | |
) | |
} | |
] : | |
[ | |
...accum, | |
{ | |
...object, | |
[prop]: value | |
} | |
], result) | |
const data = [{ | |
name: "apple", | |
children: [{ | |
name: "apple-child-1", | |
children: [{ | |
name: "apple-grandchild" | |
}] | |
}] | |
}, | |
{ | |
name: "orange" | |
} | |
] | |
console.log((addPropToArrayOfObjectsRecursive('active', false, data))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment