From 7d58dbeb5f01eda5c0b8abd0c97f519cbcb04021 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 11 Jan 2018 12:36:08 +0200 Subject: [PATCH] Delete flattenDepth.md --- snippets/flattenDepth.md | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 snippets/flattenDepth.md diff --git a/snippets/flattenDepth.md b/snippets/flattenDepth.md deleted file mode 100644 index 9f654b932..000000000 --- a/snippets/flattenDepth.md +++ /dev/null @@ -1,19 +0,0 @@ -### 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). - -```js -const flattenDepth = (arr, depth = 1) => - depth != 1 - ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), []) - : arr.reduce((a, v) => a.concat(v), []); -``` - -```js -flattenDepth([1, [2], 3, 4]); // [1,2,3,4] -```