From 690cefa43fa45156236b788e817198a4ac700c29 Mon Sep 17 00:00:00 2001 From: Parvez Khan Date: Thu, 9 Jan 2025 16:14:25 +0530 Subject: [PATCH] Update preferred array methods to include ES2023 non-mutating methods: toSpliced, with, toReversed, toSorted --- src/content/learn/updating-arrays-in-state.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/content/learn/updating-arrays-in-state.md b/src/content/learn/updating-arrays-in-state.md index 61e4f4e2d98..b17af1f8d47 100644 --- a/src/content/learn/updating-arrays-in-state.md +++ b/src/content/learn/updating-arrays-in-state.md @@ -26,11 +26,9 @@ Here is a reference table of common array operations. When dealing with arrays i | | avoid (mutates the array) | prefer (returns a new array) | | --------- | ----------------------------------- | ------------------------------------------------------------------- | -| adding | `push`, `unshift` | `concat`, `[...arr]` spread syntax ([example](#adding-to-an-array)) | -| removing | `pop`, `shift`, `splice` | `filter`, `slice` ([example](#removing-from-an-array)) | -| replacing | `splice`, `arr[i] = ...` assignment | `map` ([example](#replacing-items-in-an-array)) | -| sorting | `reverse`, `sort` | copy the array first ([example](#making-other-changes-to-an-array)) | - +| removing | `pop`, `shift`, `splice` | `toSpliced`, `filter`, `slice` ([example](#removing-from-an-array)) | +| replacing | `splice`, `arr[i] = ...` assignment | `with`, `map` ([example](#replacing-items-in-an-array)) | +| sorting | `reverse`, `sort` | `toReversed`, `toSorted`, or copy the array first ([example](#making-other-changes-to-an-array)) | Alternatively, you can [use Immer](#write-concise-update-logic-with-immer) which lets you use methods from both columns.