Sub topic 6
Replacing one element with with
This chapter assumes you know how array indexing works, how bracket assignment (arr[i] = value) changes an array, and how primitive values differ from object references. Earlier chapters covered why mutation causes bugs and how toSorted and toSpliced work. This one covers the third copying method, with.
The problem with bracket assignment
Bracket assignment is the most direct way to change one element. It is also mutating. Any other code that holds a reference to the same array sees the change.
const original = [1, 2, 3, 4, 5];
const alias = original;
alias[2] = 6;
console.log(original); // [1, 2, 6, 4, 5]
console.log(alias === original); // trueBefore ES2023, the usual way to avoid this was to copy first and then assign. That takes two steps, and it is easy to forget the copy.
const original = [1, 2, 3, 4, 5];
const copy = [...original];
copy[2] = 6;
console.log(copy); // [1, 2, 6, 4, 5]
console.log(original); // [1, 2, 3, 4, 5]How with works
arr.with(index, value) returns a new array that is the same as arr except at index, which holds value. The original array is not changed. It is the copying version of bracket assignment, in the same way that toSorted is the copying version of sort.
const values = [1, 2, 3, 4, 5];
const updated = values.with(2, 6);
console.log(updated); // [1, 2, 6, 4, 5]
console.log(values); // [1, 2, 3, 4, 5]
console.log(updated === values); // falseThe method takes exactly two arguments:
| Argument | Meaning |
|---|---|
| `index` | The position to replace. Negative values count from the end. |
| `value` | The new element to put at that position. |
The new array always has the same length as the original. with replaces an element. It never inserts or removes one. If you need to insert or remove, use toSpliced.
Because with is an expression, you can use it inline
with returns the new array, so you can use it anywhere an expression is allowed: as a return value, as a function argument, or inside a larger expression. Bracket assignment needs a statement of its own.
const setAt = (arr, i, v) => arr.with(i, v);
const board = ["", "", "", "", "", "", "", "", ""];
const afterMove = setAt(board, 4, "X");
console.log(afterMove); // ["", "", "", "", "X", "", "", "", ""]
console.log(board[4]); // ""This is why with fits well in reducers and update functions that must return a new value instead of editing the old one.
Common errors
- Expecting the original to change.
values.with(0, 9);on its own line does nothing useful. The result is thrown away. Assign it or return it. - Trying to grow the array.
arr[arr.length] = xappends an element.arr.with(arr.length, x)throws aRangeError. The next chapter covers the index rules in detail. - Assuming a deep copy. Only the array is new. Elements that are objects are still shared with the original. A later chapter on shallow copies explains what this means.
Exercises
Exercise
Given const letters = ["a", "b", "c"]; what do letters.with(1, "z") and letters print afterwards, and why?
Show answer
letters.with(1, "z") returns ["a", "z", "c"]. letters still prints ["a", "b", "c"], because with builds a new array and never modifies the array it is called on.
Exercise
Rewrite this function so it does not mutate its argument: function markFirst(list) { list[0] = "done"; return list; }
Show answer
function markFirst(list) { return list.with(0, "done"); } This returns a new array with index 0 replaced and leaves list unchanged. Note that it throws a RangeError if list is empty, where the original version would have added an element.