From b3f7c855ff19700db7ff148cc045a207aa682b79 Mon Sep 17 00:00:00 2001 From: sabareesh Date: Thu, 14 Dec 2017 16:51:34 +0530 Subject: [PATCH 1/6] lodash remove duplicated --- README.md | 14 ++++++++++++++ snippets/remove.md | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 snippets/remove.md diff --git a/README.md b/README.md index 77a5233e9..eacb3932a 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ * [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) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) @@ -610,6 +611,19 @@ const redirect = (url, asLink = true) => // redirect('https://google.com') ``` +### Remove + +Remove elements from `arr` that are returns truthy. Use `Array.filter()` to finding the matching elements and `Array.reduce()` to remove elements using `Array.splice()`.The `func` is invoked with three arguments: (value, index, array). + +```js +const remove = (arr, func) => + 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] +``` + ### Reverse a string Use array destructuring and `Array.reverse()` to reverse the order of the characters in the string. diff --git a/snippets/remove.md b/snippets/remove.md new file mode 100644 index 000000000..6c8e71d55 --- /dev/null +++ b/snippets/remove.md @@ -0,0 +1,12 @@ +### Remove + +Remove elements from `arr` that are returns truthy. Use `Array.filter()` to finding the matching elements and `Array.reduce()` to remove elements using `Array.splice()`.The `func` is invoked with three arguments: (value, index, array). + +```js +const remove = (arr, func) => + 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] +``` From a0409dbfaab355192a4355bfde48486673ea22e5 Mon Sep 17 00:00:00 2001 From: sabareesh Date: Thu, 14 Dec 2017 16:56:32 +0530 Subject: [PATCH 2/6] lodash remove duplicated --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index c8a19046c..12e86b020 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,9 @@ * [Promisify](#promisify) * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) -<<<<<<< HEAD * [Randomize order of array](#randomize-order-of-array) * [Redirect to url](#redirect-to-url) * [Remove](#remove) -======= -* [Redirect to URL](#redirect-to-url) ->>>>>>> 10ae7ad867aaa94d27da81180760d6a0fa84a3a8 * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) From dc929505c64053bdc85e7adad1f6010f31a55325 Mon Sep 17 00:00:00 2001 From: sabareesh Date: Fri, 15 Dec 2017 11:41:51 +0530 Subject: [PATCH 3/6] 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] ``` From fb5a958115329c7cc1a1e52e9043c0869e3cddfb Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 15 Dec 2017 13:50:09 +0200 Subject: [PATCH 4/6] Update remove.md --- snippets/remove.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/snippets/remove.md b/snippets/remove.md index 0a25e6f00..f9cfbc8a1 100644 --- a/snippets/remove.md +++ b/snippets/remove.md @@ -1,14 +1,13 @@ ### Remove -Remove elements from `arr` that are returns truthy. Use `Array.filter()` to finding the matching elements and `Array.reduce()` to remove elements using `Array.splice()`.The `func` is invoked with three arguments: (value, index, array). +Use `Array.filter()` to find array elements that return truthy values and `Array.reduce()` to remove elements using `Array.splice()`. +The `func` is invoked with three arguments (`value, index, array`). ```js const remove = (arr, func) => - Array.isArray(arr) ? - arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1) - return acc.concat(val); + 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] ``` From 613eabec3df0f8e4e757425bd113246f622adebb Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 15 Dec 2017 13:50:46 +0200 Subject: [PATCH 5/6] Update and rename remove.md to array-remove.md --- snippets/{remove.md => array-remove.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename snippets/{remove.md => array-remove.md} (96%) diff --git a/snippets/remove.md b/snippets/array-remove.md similarity index 96% rename from snippets/remove.md rename to snippets/array-remove.md index f9cfbc8a1..e08a4a02f 100644 --- a/snippets/remove.md +++ b/snippets/array-remove.md @@ -1,4 +1,4 @@ -### Remove +### Array remove Use `Array.filter()` to find array elements that return truthy values and `Array.reduce()` to remove elements using `Array.splice()`. The `func` is invoked with three arguments (`value, index, array`). From d84a0dad6c21b37a03640be3169e55aa20c08d27 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 15 Dec 2017 13:51:06 +0200 Subject: [PATCH 6/6] Update array-concatenation.md --- snippets/array-concatenation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/array-concatenation.md b/snippets/array-concatenation.md index f441a0a17..40bf43c29 100644 --- a/snippets/array-concatenation.md +++ b/snippets/array-concatenation.md @@ -1,6 +1,6 @@ ### Array concatenation -Use `Array.concat()` to concatenate and array with any additional arrays and/or values, specified in `args`. +Use `Array.concat()` to concatenate an array with any additional arrays and/or values, specified in `args`. ```js const ArrayConcat = (arr, ...args) => [].concat(arr, ...args);