Reputation: 262
Hello I'm new to ImmutableJs. I want to update an particular object in ImmutableJs array.
Here is my array
const todos = Immutable.fromJS([{
text: 'ADDED TODO',
id: 0,
todo:[]
},
{
text: 'DELETED TODO',
id: 1,
todo:[]
},
{
text: 'DELETED TODO',
id: 1,
todo:[]
}]);
Now I want to push an object in the second object where id is 1 inside todo empty array.
Can anyone suggest the way to do it??
Upvotes: 0
Views: 597
Reputation: 689
If your list is ordered by id
, you can just do this:
const newTodos = todos.update(
1,
map => map.update(
"todo",
list => list.push("item")
)
);
First update
find the Map
with corresponding index and apply it to a function. Second update
is similar, it finds the key with todo
and apply it to another function. Then you simply push whatever item into your todo
list and it's done.
If you are not sure that id
is corresponded to the index, replace 1
with findIndex
as follow.
todos.findIndex(todo => todo.get("id") === 1),
Upvotes: 1