JS Built-ins
← Array221 / 226
ArrayArray.prototype.withadvanced

Apply Multiple Replacements

πŸ“– Usage Example
const arr = [1, 2, 3]
arr.with(0, 10).with(2, 30)  // β†’ [10, 2, 30]

Chain with() calls to replace multiple elements in immutable pipeline style.

Array.prototype.with β€” Chaining

with() returns a new array, so it can be chained. Use reduce to apply a list of [index, value] changes one after another.

Implement setMultiple(arr, changes) where changes is an array of [index, value] tuples.

πŸ’‘ Hints (3)
  • β€’ Use `Array.prototype.reduce` to accumulate the result of repeated `.with()` calls.
  • β€’ The accumulator starts as `arr` and each step returns `current.with(index, val)`.
  • β€’ Because `.with()` is non-mutating, each step in the reduce produces a fresh array.