From 4e7085b5ba5fcb7efdc25f2a2531cbb63bf2b117 Mon Sep 17 00:00:00 2001 From: sabareesh Date: Fri, 15 Dec 2017 11:41:51 +0530 Subject: [PATCH] new arrray remove and concat issue fixed --- README.md | 13 ++++++++++++- snippets/array-concatenation.md | 2 +- snippets/remove.md | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 12e86b020..35db7870c 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ ## Contents * [Anagrams of string (with duplicates)](#anagrams-of-string-with-duplicates) +* [Array concatenation](#array-concatenation) * [Array difference](#array-difference) * [Array intersection](#array-intersection) * [Array union](#array-union) @@ -57,7 +58,6 @@ * [Promisify](#promisify) * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) -* [Randomize order of array](#randomize-order-of-array) * [Redirect to url](#redirect-to-url) * [Remove](#remove) * [Reverse a string](#reverse-a-string) @@ -97,6 +97,15 @@ const anagrams = str => { // anagrams('abc') -> ['abc','acb','bac','bca','cab','cba'] ``` +### Array concatenation + +Use `Array.concat()` to concatenate and array with any additional arrays and/or values, specified in `args`. + +```js +const ArrayConcat = (arr, ...args) => [].concat(arr, ...args); +// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]] +``` + ### Array difference Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`. @@ -618,10 +627,12 @@ Remove elements from `arr` that are returns truthy. Use `Array.filter()` to find ```js const remove = (arr, func) => + Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { arr.splice(arr.indexOf(val), 1) return acc.concat(val); }, []) + : []; //remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4] ``` diff --git a/snippets/array-concatenation.md b/snippets/array-concatenation.md index 39d978d6e..f441a0a17 100644 --- a/snippets/array-concatenation.md +++ b/snippets/array-concatenation.md @@ -3,6 +3,6 @@ Use `Array.concat()` to concatenate and array with any additional arrays and/or values, specified in `args`. ```js -const ArrayConcat = (arr, ...args) => arr.concat(...args); +const ArrayConcat = (arr, ...args) => [].concat(arr, ...args); // ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]] ``` diff --git a/snippets/remove.md b/snippets/remove.md index 6c8e71d55..0a25e6f00 100644 --- a/snippets/remove.md +++ b/snippets/remove.md @@ -4,9 +4,11 @@ Remove elements from `arr` that are returns truthy. Use `Array.filter()` to find ```js const remove = (arr, func) => + Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { arr.splice(arr.indexOf(val), 1) return acc.concat(val); }, []) + : []; //remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4] ```