diff --git a/README.md b/README.md index 0036100e1..dd825ad0c 100644 --- a/README.md +++ b/README.md @@ -768,19 +768,26 @@ filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1,3,5] ### flatten -Flattens an array. +Flattens an array up to the specified depth. -Use a new array, `Array.concat()` and the spread operator (`...`) to cause a shallow denesting of any contained arrays. +Use recursion, decrementing `depth` by 1 for each level of depth. +Use `Array.reduce()` and `Array.concat()` to merge elements or arrays. +Base case, for `depth` equal to `1` stops recursion. +Omit the second argument, `depth` to flatten only to a depth of `1` (single flatten). ```js -const flatten = arr => [].concat(...arr); +const flatten = (arr, depth = 1) => + depth != 1 + ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), []) + : arr.reduce((a, v) => a.concat(v), []); ```
Examples ```js -flatten([1, [2], 3, 4]); // [1,2,3,4] +flatten([1, [2], 3, 4]); // [1, 2, 3, 4] +flatten([1, [2, [3, [4, 5], 6], 7], 8], 2); // [1, 2, 3, [4, 5], 6, 7, 8] ```
@@ -5201,6 +5208,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { + const newPost = { "userId": 1, "id": 1337, diff --git a/docs/index.html b/docs/index.html index 53d6278d4..3dfa73629 100644 --- a/docs/index.html +++ b/docs/index.html @@ -119,8 +119,12 @@ Object.assig
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]
 

filterNonUnique

Filters out the non-unique values in an array.

Use Array.filter() for an array containing only the unique values.

const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
 
filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1,3,5]
-

flatten

Flattens an array.

Use a new array, Array.concat() and the spread operator (...) to cause a shallow denesting of any contained arrays.

const flatten = arr => [].concat(...arr);
-
flatten([1, [2], 3, 4]); // [1,2,3,4]
+

flatten

Flattens an array up to the specified depth.

Use recursion, decrementing depth by 1 for each level of depth. Use Array.reduce() and Array.concat() to merge elements or arrays. Base case, for depth equal to 1 stops recursion. Omit the second argument, depth to flatten only to a depth of 1 (single flatten).

const flatten = (arr, depth = 1) =>
+  depth != 1
+    ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), [])
+    : arr.reduce((a, v) => a.concat(v), []);
+
flatten([1, [2], 3, 4]); // [1, 2, 3, 4]
+flatten([1, [2, [3, [4, 5], 6], 7], 8], 2); // [1, 2, 3, [4, 5], 6, 7, 8]
 

flattenDepth

Flattens an array up to the specified depth.

Use recursion, decrementing depth by 1 for each level of depth. Use Array.reduce() and Array.concat() to merge elements or arrays. Base case, for depth equal to 1 stops recursion. Omit the second element, depth to flatten only to a depth of 1 (single flatten).

const flattenDepth = (arr, depth = 1) =>
   depth != 1
     ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
@@ -1163,6 +1167,7 @@ Logs: {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,
diff --git a/snippets/httpPost.md b/snippets/httpPost.md
index 9387c3afb..4777f624a 100644
--- a/snippets/httpPost.md
+++ b/snippets/httpPost.md
@@ -24,6 +24,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,