Created
February 9, 2018 09:33
-
-
Save tswistak/df8b71e79222106c53d0f7d487239de7 to your computer and use it in GitHub Desktop.
Quick JS collections snippets
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
| /* | |
| * Snippets collected during development: made by myself or found on StackOverflow/GitHub. | |
| * Collection will grow in time. | |
| * Enjoy. | |
| */ | |
| // --- removing duplicates from an array --- | |
| [2, 4, 5, 2, 6, 4].filter((el, i, arr) => arr.indexOf(el) === i); | |
| [2, 4, 5, 2, 6, 4].reduce((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], []); | |
| [2, 4, 5, 2, 6, 4].reduce((acc, curr) => acc.includes(curr) ? acc : acc.concat(curr), []); | |
| [...new Set([2, 4, 5, 2, 6, 4])]; | |
| // --- flattening array of arrays --- | |
| [[2,4], [5,2,6], [4]].reduce((acc, curr) => acc.concat(curr)); | |
| // --- last element of array --- | |
| const arr = [2, 4, 5, 2, 6, 4]; | |
| arr[arr.length - 1]; | |
| const [last] = arr.splice(-1); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment