Back to Immutable Js

update()

website/docs/update().mdx

5.1.5777 B
Original Source

import Repl from '@/repl/Repl.tsx'; import CodeLink from '@/mdx-components/CodeLink.tsx';

update()

Returns a copy of the collection with the value at key set to the result of providing the existing value to the updating function.

A functional alternative to collection.update(key, fn) which will also work with plain Objects and Arrays as an alternative for collectionCopy[key] = fn(collection[key]).

<Signature code={update(collection: C, key, updater): C;} />

<Repl defaultValue={const originalArray = List(['dog', 'frog', 'cat']); update(originalArray, 1, (val) => val.toUpperCase()); // [ 'dog', 'FROG', 'cat' ]} />

<Repl defaultValue={const originalObject = { x: 123, y: 456 }; update(originalObject, 'x', (val) => val * 6); // { x: 738, y: 456 }} />