Last active
September 5, 2022 13:33
-
-
Save nickserv/8812a9a1e4cbae14897d8a2b1fe64c44 to your computer and use it in GitHub Desktop.
repeatItem
This file contains 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
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