-
-
Save turovskiy/134349a0b1a8f3e88cc3579cc60d04ae to your computer and use it in GitHub Desktop.
Revisions
-
jherr revised this gist
May 30, 2023 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ Frameworks like React require that when you change the contents of an array or object you change its reference. Or push another way that you don't change arrays but instead create new arrays with updated values (i.e. immutability). There are older array methods that are incompatible with immutability because they alter the array in place and don't change the array reference. These are _mutable_ (or _destructive_) methods. Shown below are replacements for the array destructive methods (e.g. `push`, `pop`, `splice`, `sort`, etc.) that will create new array references with the updated data. -
jherr revised this gist
Apr 24, 2023 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -95,7 +95,7 @@ Destructive version: ```js const myArray = [1, 3, 2]; const shiftedValue = myArray.shift(); // [3, 2] ``` Non-destructive version: @@ -111,7 +111,7 @@ Destructive version: ```js const myArray = [1, 3, 2]; myArray.unshift(6); // [6, 1, 3, 2] ``` Non-destructive versions: @@ -134,7 +134,7 @@ Destructive version: ```js const myArray = [1, 3, 2]; myArray.splice(1, 1, 4, 5, 6); // [1, 4, 5, 6, 2] ``` Non-destructive versions: @@ -161,7 +161,7 @@ Destructive version: ```js const myArray = [1, 3, 2]; myArray.reverse(); // [2, 3, 1] ``` Non-destructive versions: @@ -184,7 +184,7 @@ Destructive version: ```js const myArray = [1, 3, 2]; myArray.sort(); // [1, 2, 3] ``` Non-destructive versions: -
jherr revised this gist
Apr 24, 2023 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ Destructive version: ```js const myArray = [1, 3, 2]; myArray[1] = 10; // [1, 10, 2] ``` Non-destructive versions: @@ -47,7 +47,7 @@ Destructive version: ```js const myArray = [1, 3, 2]; myArray.push(5); // [1, 3, 2, 5] ``` Non-destructive versions: @@ -70,7 +70,7 @@ Destructive version: ```js const myArray = [1, 3, 2]; const poppedValue = myArray.pop(); // [1, 3] ``` Non-destructive versions: -
jherr revised this gist
Apr 24, 2023 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ Shown below are replacements for the array destructive methods (e.g. `push`, `po Solutions are provided using the spread operator and also the newer ["change array by copy"](https://github.com/tc39/proposal-change-array-by-copy) methods (`toSpliced`, `toSorted`, `toReversed` and `with`). ## Setting Value At Index Destructive version: @@ -41,7 +41,7 @@ const myArray = [1, 3, 2]; const newArray = myArray.with(1, 10); ``` ## Push Destructive version: @@ -64,7 +64,7 @@ const myArray = [1, 3, 2]; const newArray = myArray.toSpliced(myArray.length, 0, 5); ``` ## Pop Destructive version: @@ -89,7 +89,7 @@ const poppedValue = myArray[myArray.length - 1]; const newArray = myArray.toSpliced(myArray.length - 1, 1); ``` ## Shift Destructive version: @@ -105,7 +105,7 @@ const myArray = [1, 3, 2]; const [shiftedValue, ...newArray] = myArray; ``` ## Unshift Destructive version: @@ -128,7 +128,7 @@ const myArray = [1, 3, 2]; const newArray = myArray.toSpliced(0, 0, 6); ``` ## Splice Destructive version: @@ -155,7 +155,7 @@ const myArray = [1, 3, 2]; const newArray = myArray.toSpliced(1, 1, 4, 5, 6); ``` ## Reverse Destructive version: @@ -178,7 +178,7 @@ const myArray = [1, 3, 2]; const newArray = myArray.toReversed(); ``` ## Sort Destructive version: -
jherr revised this gist
Apr 24, 2023 . 1 changed file with 12 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ Frameworks like React require that when you change the contents of an array or object you change it's reference. Or push another way that you don't change arrays but instead create new arrays with updated values (i.e. immutability). There are older array methods that are incompatible with immutability because they alter the array in place and don't cghange the array reference. These are _destructive_ methods. Shown below are replacements for the array destructive methods (e.g. `push`, `pop`, `splice`, `sort`, etc.) that will create new array references with the updated data. Solutions are provided using the spread operator and also the newer ["change array by copy"](https://github.com/tc39/proposal-change-array-by-copy) methods (`toSpliced`, `toSorted`, `toReversed` and `with`). @@ -12,7 +15,7 @@ const myArray = [1, 3, 2]; myArray[1] = 10; ``` Non-destructive versions: ```js const myArray = [1, 3, 2]; @@ -47,7 +50,7 @@ const myArray = [1, 3, 2]; myArray.push(5); ``` Non-destructive versions: ```js const myArray = [1, 3, 2]; @@ -70,7 +73,7 @@ const myArray = [1, 3, 2]; const poppedValue = myArray.pop(); ``` Non-destructive versions: ```js const myArray = [1, 3, 2]; @@ -111,7 +114,7 @@ const myArray = [1, 3, 2]; myArray.unshift(6); ``` Non-destructive versions: ```js const myArray = [1, 3, 2]; @@ -134,7 +137,7 @@ const myArray = [1, 3, 2]; myArray.splice(1, 1, 4, 5, 6); ``` Non-destructive versions: ```js const myArray = [1, 3, 2]; @@ -161,7 +164,7 @@ const myArray = [1, 3, 2]; myArray.reverse(); ``` Non-destructive versions: ```js const myArray = [1, 3, 2]; @@ -184,7 +187,7 @@ const myArray = [1, 3, 2]; myArray.sort(); ``` Non-destructive versions: ```js const myArray = [1, 3, 2]; -
jherr revised this gist
Apr 24, 2023 . 1 changed file with 22 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,27 +3,39 @@ create new array references with the updated data. Solutions are provided using the spread operator and also the newer ["change array by copy"](https://github.com/tc39/proposal-change-array-by-copy) methods (`toSpliced`, `toSorted`, `toReversed` and `with`). ### Setting Value At Index Destructive version: ```js const myArray = [1, 3, 2]; myArray[1] = 10; ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const newArray = [...myArray]; newArray[1] = 10; ``` Or: ```js const myArray = [1, 3, 2]; const newArray = [ ...myArray.slice(0, 1), 10, ...myArray.slice(2, myArray.length) ]; ``` Or (if you have access to `with`): ```js const myArray = [1, 3, 2]; const newArray = myArray.with(1, 10); ``` ### Push @@ -163,37 +175,25 @@ const myArray = [1, 3, 2]; const newArray = myArray.toReversed(); ``` ### Sort Destructive version: ```js const myArray = [1, 3, 2]; myArray.sort(); ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const newArray = [...myArray].sort(); ``` Or (if you have access to `toSorted`): ```js const myArray = [1, 3, 2]; const newArray = myArray.toSorted(); ``` -
jherr revised this gist
Apr 24, 2023 . 1 changed file with 25 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ These are replacements for the array destructive methods (e.g. push, pop, splice, sort, etc.) that will create new array references with the updated data. Solutions are provided using the spread operator and also the newer ["change array by copy"](https://github.com/tc39/proposal-change-array-by-copy) methods (`toSpliced`, `toSorted`, `toReversed` and `with`). ### Sort Destructive version: @@ -138,6 +140,29 @@ const myArray = [1, 3, 2]; const newArray = myArray.toSpliced(1, 1, 4, 5, 6); ``` ### Reverse Destructive version: ```js const myArray = [1, 3, 2]; myArray.reverse(); ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const newArray = [...myArray].reverse(); ``` Or (if you have access to `toReversed`): ```js const myArray = [1, 3, 2]; const newArray = myArray.toReversed(); ``` ### Setting Value At Index Destructive version: -
jherr created this gist
Apr 24, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,174 @@ These are replacements for the array destructive methods (e.g. push, pop, splice, sort, etc.) that will create new array references with the updated data. ### Sort Destructive version: ```js const myArray = [1, 3, 2]; myArray.sort(); ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const newArray = [...myArray].sort(); ``` Or (if you have access to `toSorted`): ```js const myArray = [1, 3, 2]; const newArray = myArray.toSorted(); ``` ### Push Destructive version: ```js const myArray = [1, 3, 2]; myArray.push(5); ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const newArray = [...myArray, 5]; ``` Or (if you have access to `toSpliced`): ```js const myArray = [1, 3, 2]; const newArray = myArray.toSpliced(myArray.length, 0, 5); ``` ### Pop Destructive version: ```js const myArray = [1, 3, 2]; const poppedValue = myArray.pop(); ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const poppedValue = myArray[myArray.length - 1]; const newArray = myArray.slice(0, myArray.length - 1); ``` Or (if you have access to `toSpliced`): ```js const myArray = [1, 3, 2]; const poppedValue = myArray[myArray.length - 1]; const newArray = myArray.toSpliced(myArray.length - 1, 1); ``` ### Shift Destructive version: ```js const myArray = [1, 3, 2]; const shiftedValue = myArray.shift(); ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const [shiftedValue, ...newArray] = myArray; ``` ### Unshift Destructive version: ```js const myArray = [1, 3, 2]; myArray.unshift(6); ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const newArray = [6, ...myArray]; ``` Or (if you have access to `toSpliced`): ```js const myArray = [1, 3, 2]; const newArray = myArray.toSpliced(0, 0, 6); ``` ### Splice Destructive version: ```js const myArray = [1, 3, 2]; myArray.splice(1, 1, 4, 5, 6); ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const newArray = [ ...myArray.slice(0, 1), 4, 5, 6, ...myArray.slice(2, myArray.length) ]; ``` Or (if you have access to `toSpliced`): ```js const myArray = [1, 3, 2]; const newArray = myArray.toSpliced(1, 1, 4, 5, 6); ``` ### Setting Value At Index Destructive version: ```js const myArray = [1, 3, 2]; myArray[1] = 10; ``` Non-destructive version: ```js const myArray = [1, 3, 2]; const newArray = [...myArray]; newArray[1] = 10; ``` Or: ```js const myArray = [1, 3, 2]; const newArray = [ ...myArray.slice(0, 1), 10, ...myArray.slice(2, myArray.length) ]; ``` Or (if you have access to `with`): ```js const myArray = [1, 3, 2]; const newArray = myArray.with(1, 10); ```