How to update element inside List with ImmutableJS?

后端 未结 7 1144
面向向阳花
面向向阳花 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:34

    Here is what official docs said… updateIn

    You don't need updateIn, which is for nested structures only. You are looking for the update method, which has a much simpler signature and documentation:

    Returns a new List with an updated value at index with the return value of calling updater with the existing value, or notSetValue if index was not set.

    update(index: number, updater: (value: T) => T): List
    update(index: number, notSetValue: T, updater: (value: T) => T): List
    

    which, as the Map::update docs suggest, is "equivalent to: list.set(index, updater(list.get(index, notSetValue)))".

    where element with name "third"

    That's not how lists work. You have to know the index of the element that you want to update, or you have to search for it.

    How can I update list where element with name third have its count set to 4?

    This should do it:

    list = list.update(2, function(v) {
        return {id: v.id, name: v.name, count: 4};
    });
    

提交回复
热议问题