How to update element inside List with ImmutableJS?

后端 未结 7 1133
面向向阳花
面向向阳花 2020-12-04 09:02

Here is what official docs said

updateIn(keyPath: Array, updater: (value: any) => any): List
updateIn(keyPath: Array, notSe         


        
相关标签:
7条回答
  • 2020-12-04 09:36

    With .setIn() you can do the same:

    let obj = fromJS({
      elem: [
        {id: 1, name: "first", count: 2},
        {id: 2, name: "second", count: 1},
        {id: 3, name: "third", count: 2},
        {id: 4, name: "fourth", count: 1}
      ]
    });
    
    obj = obj.setIn(['elem', 3, 'count'], 4);
    

    If we don’t know the index of the entry we want to update. It’s pretty easy to find it using .findIndex():

    const indexOfListToUpdate = obj.get('elem').findIndex(listItem => {
      return listItem.get('name') === 'third';
    });
    obj = obj.setIn(['elem', indexOfListingToUpdate, 'count'], 4);
    

    Hope it helps!

    0 讨论(0)
提交回复
热议问题