[UPDATE] Flatten updated to use ES6 spread
This commit is contained in:
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
Flattens an array.
|
Flattens an array.
|
||||||
|
|
||||||
Use `Array.reduce()` to get all elements inside the array and `concat()` to flatten them.
|
Using a new array we concatinate it with the spread input array causing a shallow flatten
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
|
const flatten = arr => [ ].concat( ...arr );
|
||||||
// flatten([1,[2],3,4]) -> [1,2,3,4]
|
// flatten([1,[2],3,4]) -> [1,2,3,4]
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user