I am looking to replace a fragment of my JSON object with another part using immutable.js.
Consider the example below where we have a more complex and nested object. I want to update/replace the node with "id": "F"
with a different edited node.
As per the documentation, I need to extract the `keyPath` as an array and pass it to the function
setIn(keyPath: Array<any>, value: any): Map<K, V>
.
Therefore, I need to find the keypath for F: [ 'children', 2, 'children', 0, 'children', 0 ] (is this correct?).
This is just a simplified example. In my scenario, the keypath has a much more elaborate hierarchy. How can I achieve this using immutable.js or plain JS? Is this approach accurate? Are there more elegant solutions to achieve the same behavior (preferably utilizing immutable.js functions)?
Best regards,
Rafal
Example:
{
"id": "A",
"children": [
{
"id": "B",
"children": [
]
},
{
"id": "C",
"children": [
]
},
{
"id": "D",
"children": [
{
"id": "E",
"children": [
{
"id": "F", // THIS NODE WILL BE UPDATED
"children": [
]
}
]
},
{
"id": "G",
"children": [
]
}
]
}
]
}