Created
October 1, 2019 20:20
-
-
Save parkerziegler/70bd6be34f7790c1baad70c4bcb508c9 to your computer and use it in GitHub Desktop.
Intersperse items, including React elements, with a separator.
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
/** | |
* Modified from Sophie's StackOverflow answer here: | |
* https://stackoverflow.com/questions/23618744/rendering-comma-separated-list-of-links | |
*/ | |
function intersperse<T>(arr: T[], sep: any) { | |
if (arr.length === 0) { | |
return []; | |
} | |
return arr.slice(1).reduce((acc, el) => acc.concat([sep, el]), [arr[0]]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment