Last active
January 9, 2024 23:46
-
-
Save hk-skit/a82c4d9bf1c95f066e4f7e37edf3c81d to your computer and use it in GitHub Desktop.
Useful Array One-liners.
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
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const merge = {...arrayA, ...arrayB}