diff --git a/README.md b/README.md index b68f397e5..8556ec300 100644 --- a/README.md +++ b/README.md @@ -483,10 +483,10 @@ const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexO Flattens an array. -Use `Array.reduce()` to get all elements inside the array and `concat()` to flatten them. +Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays. ```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] ``` diff --git a/docs/index.html b/docs/index.html index dc8296a8e..0c1ea6384 100644 --- a/docs/index.html +++ b/docs/index.html @@ -367,8 +367,8 @@ Returns the remaining elements.
Flattens an array.
-Use Array.reduce() to get all elements inside the array and concat() to flatten them.
const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
+Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays.
+const flatten = arr => [ ].concat( ...arr );
// flatten([1,[2],3,4]) -> [1,2,3,4]