update(object, path, update) ⇒ Object | Array.<any>

Sometimes we need to set new value depends on previous value. Here update() operation can help.

Returns: Object | Array.<any> - updated object Params

  • object Object | Array.<any> - object to update
  • path string | Array.<string> - path to be updated (array of items or dot-separated string can be provided)
  • update function - function to update value

Description

import { update } from 'immutable-object-update';

const state = {
    a: {
        a1: 1,
        a2: 2
    },
    b: {
        b1: 3,
        b2: 4
    }
};

const increment = oldValue => oldValue + 1;
const updated = update(state, [ 'b', 'b1' ], increment);

// or

const updated = update(state, 'b.b1', increment);

As a result we will receive new object with structure below:

{
    a: {
        a1: 1,
        a2: 2
    },
    b: {
        b1: 4,
        b2: 5
    }
}

results matching ""

    No results matching ""