This library was inspired by and based on find-replace, does not mutate an initial array and built on top of TypeScript
If the replacements
value is a plain value, it will be used found values will be replaced by it:
import { immutableFindReplace } from 'immutable-find-replace';
const initialArray = [1, 2, 3, 4, 5];
const result = immutableFindReplace(initialArray, x => x === 5, 10);
console.log(result);
// [1, 2, 3, 4, 10]
If the replacements
value is a function, it will be invoked with the found item and its result used as the replace value:
import { immutableFindReplace } from 'immutable-find-replace';
const initialArray = [1, 2, 3, 4, 5];
const result = immutableFindReplace(initialArray, x => x === 5, x => x * 20);
console.log(result);
// [1, 2, 3, 4, 100]
If the replacements
isn't provided then found values will be deleted from the initial array:
import { immutableFindReplace } from 'immutable-find-replace';
const initialArray = [1, 2, 3, 4, 5];
const result = immutableFindReplace(initialArray, x => x > 1 && x < 5);
console.log(result);
// [1, 5]
If the initialArray
does not contain required value, nothing will be changed:
import { immutableFindReplace } from 'immutable-find-replace';
const initialArray = [1, 2, 3, 4, 5];
const result = immutableFindReplace(initialArray, x => x === 6, 10);
console.log(result);
// [1, 2, 3, 4, 5]