Files
30-seconds-of-code/snippets/join.md
Rohit Tanwar 6ac6e5a5ed Update join.md
2018-01-01 16:23:31 +05:30

754 B

join

Is like Array.join() but with an additional argument of end(is equal to separator by default) which is used to separate the second to last and last items in the array. Returns "" when the array is empty and the first item when the length of array is 1.

const join = (arr = [],separator = ',',end = separator ) => {
      return arr.reduce((acc,val,i) => {
       return  i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator
     },'')
 }
join(); // ''
join(['pen','pineapple','apple','pen'],',','&'); //"pen,pineapple,apple&pen"
join(['pen','pineapple','apple','pen'],','); //"pen,pineapple,apple,pen"
join(['pen','pineapple','apple','pen']); //"pen,pineapple,apple,pen"