Created
September 2, 2020 10:36
-
-
Save watab0shi/4ef669aafed95d7ad42cdd4e11088452 to your computer and use it in GitHub Desktop.
Loop back/forward Array
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 characters
| // loop back | |
| const arr1 = [0, 1, 2, 3, 4]; | |
| arr1.splice(0, 0, arr1.slice(-1)[0]); | |
| arr1.splice(-1, 1); | |
| console.log(arr1);// [4, 0, 1, 2, 3] | |
| // loop forward | |
| const arr2 = [0, 1, 2, 3, 4]; | |
| arr2.splice(arr2.length, 0, arr2.slice(0)[0]); | |
| arr2.splice(0, 1); | |
| console.log(arr2);// >> [1, 2, 3, 4, 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment