Skip to content

Instantly share code, notes, and snippets.

@nickserv
Last active September 5, 2022 13:33
Show Gist options
  • Save nickserv/8812a9a1e4cbae14897d8a2b1fe64c44 to your computer and use it in GitHub Desktop.
Save nickserv/8812a9a1e4cbae14897d8a2b1fe64c44 to your computer and use it in GitHub Desktop.
repeatItem
const items = ['a', 'b', 'c', 'd']
const repeatItem = (item, times) => Array(times + 1).fill(item)
console.log(items.reduce((memo, item, index) => memo.concat(repeatItem(item, index)), []))
// ["a", "b", "b", "c", "c", "c", "d", "d", "d", "d"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment