Skip to content

Instantly share code, notes, and snippets.

@parkerziegler
Created October 1, 2019 20:20
Show Gist options
  • Save parkerziegler/70bd6be34f7790c1baad70c4bcb508c9 to your computer and use it in GitHub Desktop.
Save parkerziegler/70bd6be34f7790c1baad70c4bcb508c9 to your computer and use it in GitHub Desktop.
Intersperse items, including React elements, with a separator.
/**
* 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