From 5f7add56646d421d977af162498a81a1c8559e4f Mon Sep 17 00:00:00 2001 From: King Date: Thu, 14 Dec 2017 01:19:15 -0500 Subject: [PATCH 1/4] add compact.md & ran npm run build-list --- README.md | 11 ++++++++++- snippets/compact.md | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 snippets/compact.md diff --git a/README.md b/README.md index ddd428064..3d2cc38a8 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ * [Chain asynchronous functions](#chain-asynchronous-functions) * [Check for palindrome](#check-for-palindrome) * [Chunk array](#chunk-array) +* [Compact](#compact) * [Count occurrences of a value in array](#count-occurrences-of-a-value-in-array) * [Current URL](#current-url) * [Curry](#curry) @@ -55,7 +56,7 @@ * [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) +* [Redirect to url](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) @@ -195,6 +196,14 @@ const chunk = (arr, size) => // chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5] ``` +### Compact + +Use `.filter()` to filter falsey values. For example false, null, 0, "", undefined, and NaN are falsey. + +```js +const compact = (arr) => arr.filter( v => [0, null, false, '', undefined].indexOf(v) === -1 && v); +// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ] +``` ### Count occurrences of a value in array Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array. diff --git a/snippets/compact.md b/snippets/compact.md new file mode 100644 index 000000000..de76f834b --- /dev/null +++ b/snippets/compact.md @@ -0,0 +1,8 @@ +### Compact + +Use `.filter()` to filter falsey values. For example false, null, 0, "", undefined, and NaN are falsey. + +```js +const compact = (arr) => arr.filter( v => [0, null, false, '', undefined].indexOf(v) === -1 && v); +// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ] +``` \ No newline at end of file From e8347a46782b68734138d79917ab6793b22ca91e Mon Sep 17 00:00:00 2001 From: King Date: Thu, 14 Dec 2017 02:56:47 -0500 Subject: [PATCH 2/4] refactor & elegant solution --- snippets/compact.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/compact.md b/snippets/compact.md index de76f834b..92dd9b767 100644 --- a/snippets/compact.md +++ b/snippets/compact.md @@ -3,6 +3,6 @@ Use `.filter()` to filter falsey values. For example false, null, 0, "", undefined, and NaN are falsey. ```js -const compact = (arr) => arr.filter( v => [0, null, false, '', undefined].indexOf(v) === -1 && v); +const compact = (arr) => arr.filter(v => v); // compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ] ``` \ No newline at end of file From 0c5f677688a5ea8ae598e27ea7b879ccb6db0d0a Mon Sep 17 00:00:00 2001 From: King Date: Thu, 14 Dec 2017 03:00:29 -0500 Subject: [PATCH 3/4] ran npm run build-list --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d2cc38a8..31007af89 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ const chunk = (arr, size) => Use `.filter()` to filter falsey values. For example false, null, 0, "", undefined, and NaN are falsey. ```js -const compact = (arr) => arr.filter( v => [0, null, false, '', undefined].indexOf(v) === -1 && v); +const compact = (arr) => arr.filter(v => v); // compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ] ``` ### Count occurrences of a value in array From 2903ece39bbe619e347a2d26ab3d54827bc4dcea Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:06:13 +0200 Subject: [PATCH 4/4] Update compact.md Updated description a little. --- snippets/compact.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/compact.md b/snippets/compact.md index 92dd9b767..607634b4a 100644 --- a/snippets/compact.md +++ b/snippets/compact.md @@ -1,8 +1,8 @@ ### Compact -Use `.filter()` to filter falsey values. For example false, null, 0, "", undefined, and NaN are falsey. +Use `Array.filter()` to filter out falsey values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`). ```js const compact = (arr) => arr.filter(v => v); // compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ] -``` \ No newline at end of file +```