Skip to content

Instantly share code, notes, and snippets.

@tswistak
Created February 9, 2018 09:33
Show Gist options
  • Save tswistak/df8b71e79222106c53d0f7d487239de7 to your computer and use it in GitHub Desktop.
Save tswistak/df8b71e79222106c53d0f7d487239de7 to your computer and use it in GitHub Desktop.
Quick JS collections snippets
/*
* 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