Can't update in deep array with a map
eldina07 opened this issue · 1 comments
eldina07 commented
Hello there,
I have a problem to update all objects in a deep array.
My structure of elements is:
{
"name":"",
"category":"",
"notes":"",
"sections":[
{
"name":"Workout",
"exercises":[
{
"sets":[
{
"value":5,
"workload":100,
"workload_type":"kg",
"timer":120,
"repetitions":[
5, 3
]
},
{
"value":5,
"workload":100,
"workload_type":"kg",
"timer":120,
"repetitions":[
5, 2
]
}
],
}
]
}
]
}I want to add a repetition in all repetitions keys in each set, so i do:
state.updateIn(
['sections', index_section, 'exercises', index_exercise, 'sets'],
sets => sets.map(s => s.update('repetitions', repetitions => repetitions = 5))
)But i have an error: s.update is not a function. Variable s is not an immutable object but i don't know why.
chrapkowski commented
I see no issue with your code:
const state = Immutable.fromJS({
"name":"",
"category":"",
"notes":"",
"sections":[
{
"name":"Workout",
"exercises":[
{
"sets":[
{
"value":5,
"workload":100,
"workload_type":"kg",
"timer":120,
"repetitions":[
5, 3
]
},
{
"value":5,
"workload":100,
"workload_type":"kg",
"timer":120,
"repetitions":[
5, 2
]
}
],
}
]
}
]
})
const afterState = state.updateIn(
["sections", 0, "exercises", 0, "sets"],
sets => sets.map(s => s.update("repetitions", repetitions => repetitions.map(repetition => repetition + 5)
)))
console.log(afterState.toJS())
https://jsfiddle.net/L87zyrtg/
Maybe index_section or index_exercise is out of range. Additionally from update function you need to return a new value instead of assigning it (I assumed you want to increase the number of repetitions by 5).