Update join.md as suggested by @atomiks
This commit is contained in:
@ -3,11 +3,8 @@
|
|||||||
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.
|
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,separator = ',',end = separator ) => {
|
const join = (arr, separator = ',', end = separator) =>
|
||||||
return arr.reduce((acc,val,i) => {
|
arr.slice(0, arr.length - 1).join(separator) + end + arr[arr.length - 1]
|
||||||
return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator
|
|
||||||
},'')
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
Reference in New Issue
Block a user