add examples for join

This commit is contained in:
Rohit Tanwar
2018-01-01 15:53:26 +05:30
parent 1cff143dfc
commit 4fbdd2ed76

View File

@ -1,11 +1,11 @@
### join ### 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.
```js ```js
const join = (arr,start = ',',end = start) => { const join = (arr,separator = ',',end = separator ) => {
return arr.reduce((acc,val,i) => { return arr.reduce((acc,val,i) => {
return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + start return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator
},'') },'')
} }
``` ```