Here is a function that I need help with:
range: [number, number] = [135, 145]
update(property: string, path: string, range: [number, number]) {
if (path.split('|').length === 2) {
const steps = path.split('|')
const addressSteps = steps.slice(0, -1)
const lastStep = addressSteps[addressSteps.length - 1]
const reducer = (accu, step) => accu[step]
// We get to the branch you need to update
const stateBranch = addressSteps.reduce(reducer, state)
stateBranch[lastStep] = {
...stateBranch[lastStep],
[property]: range,
}
}),
)
}
When I call this function...
update(bHbMaleMass, 'haematology|cbc', [135, 145])
...the state gets updated as shown in the screenshot provided.
https://i.sstatic.net/ebons.png
One issue to note is:
- The haematology node appears duplicated within the cbc node with the correct updates.
I aim to solely update the cbc node without any duplicates of haematology or cbc nodes.
Thank you for your assistance
Note: This query was previously posted at [Looking for a better way to structure code closed, but it relates specifically to one of the solutions suggested in the response.