reduce(object, path, reducer) ⇒ Object | Array.<any>
one of additional functions to work with array items
Returns: Object | Array.<any> - updated object
Params
- object
Object|Array.<any>- object to update - path
string|Array.<string>- path to be updated - reducer
function- reducer function
Description
import { reduce } from 'immutable-object-update';
const state = {
a: {
b: [ 1, 2, 3, 4, 5 ]
}
};
const sum = (acc, item) => acc + item;
const updated = reduce(state, [ 'a', 'b' ], sum);
// or
const updated = reduce(state, 'a.b', sum);
As a result we will receive new object with structure below:
{
a: {
b: 15
}
}
Please note: first element of array will be always used as initial value