Skip to content

Instantly share code, notes, and snippets.

@jurassix
Created April 11, 2016 13:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jurassix/21efce844fa0718641452647b603d97d to your computer and use it in GitHub Desktop.
Save jurassix/21efce844fa0718641452647b603d97d to your computer and use it in GitHub Desktop.
export const joinArray = (list = [], separator) =>
list.reduce((joinedList, item, index, {length}) => {
joinedList.push(item);
if (index < (length - 1)) {
joinedList.push(separator);
}
return joinedList;
}, []);
@jurassix
Copy link
Author

let list = [1, 2, 3, 4];
let joinedList = joinArray(list, 0);
console.log(joinedList);
// [1, 0, 2, 0, 3, 0, 4] 

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