Skip to content

Instantly share code, notes, and snippets.

@electerious
Created May 10, 2017 14:25
Show Gist options
  • Save electerious/961fbc62cc396bc086950b3ca661d9d1 to your computer and use it in GitHub Desktop.
Save electerious/961fbc62cc396bc086950b3ca661d9d1 to your computer and use it in GitHub Desktop.
Duplicate array items based on an array
const duplicate = (arr, duplications) => arr.reduce((acc, val, i) => {
const duplication = duplications[i]
for (let i = 0; i<duplication; i++) acc.push(val)
return acc
}, [])
@electerious
Copy link
Author

electerious commented May 10, 2017

Example:

duplicate(['a', 'b', 'c'], [0, 1, 2] // ['b', 'c', 'c']
duplicate(['a', 'b', 'c'], [0, 3, 0] // ['b', 'b', 'b']

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