Skip to content

Instantly share code, notes, and snippets.

@hk-skit
Last active January 9, 2024 23:46
Show Gist options
  • Save hk-skit/a82c4d9bf1c95f066e4f7e37edf3c81d to your computer and use it in GitHub Desktop.
Save hk-skit/a82c4d9bf1c95f066e4f7e37edf3c81d to your computer and use it in GitHub Desktop.
Useful Array One-liners.
// Remove Duplicates from an array
const removeDuplicates =
arr => arr.filter((item, index) => index === arr.indexOf(item));
const removeDuplicates1 = array => [...new Set(array)];
const removeDuplicates2 = array => Array.from(new Set(array));
// Flattens an array(doesn't flatten deeply).
const flatten = array => [].concat(...array);
const flattenDeep =
arr => arr.reduce((fArr, item) =>
fArr.concat(Array.isArray(item) ? flatten(item) : item), []);
// Merge arrays
const merge = (...arrays) => [].concat(...arrays);
// Pipe fn
const pipe = (...fns) => arg => fns.reduce((v, fn) => fn(v), arg);
// Generates range [start, end) with step
const range = (start, end, step = 1) =>
Array.from({ length: Math.ceil((end - start) / step) }, (_, i) => start + i * step);
// Generates random hex color code.
const color = () => '#' + Math.floor(Math.random() * 0xffffff).toString(16);
@celestial-labs
Copy link

const merge = {...arrayA, ...arrayB}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment