From e0377140c4435ba1ff043e2de7f948ea61791433 Mon Sep 17 00:00:00 2001 From: Swaraj Singh Date: Thu, 2 Jan 2025 11:51:42 +0530 Subject: [PATCH 1/3] Added a snippet to split an array into two arrays based on a callback function --- .../partition-an-array-into-two.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/javascript/array-manipulation/partition-an-array-into-two.md diff --git a/snippets/javascript/array-manipulation/partition-an-array-into-two.md b/snippets/javascript/array-manipulation/partition-an-array-into-two.md new file mode 100644 index 00000000..cc26fd06 --- /dev/null +++ b/snippets/javascript/array-manipulation/partition-an-array-into-two.md @@ -0,0 +1,19 @@ +--- +title: Partition Array +description: Splits an array into two arrays based on a callback function. +author: Swaraj-Singh-30 +tags: javascript,array,partition,reduce,utility +--- + +```js +const partition = (arr, callback) => + arr.reduce( + ([pass, fail], elem) => (callback(elem) ? [[...pass, elem], fail] : [pass, [...fail, elem]]), + [[], []] + ); + +// Usage: +const numbers = [1, 2, 3, 4, 5, 6]; +const isEven = (n) => n % 2 === 0; +console.log(partition(numbers, isEven)); // Output: [[2, 4, 6], [1, 3, 5]] +``` \ No newline at end of file From 51de911e7ff1ae447b888a6e942c2a1b2e402256 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 2 Jan 2025 06:22:12 +0000 Subject: [PATCH 2/3] Update consolidated snippets --- public/consolidated/javascript.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/public/consolidated/javascript.json b/public/consolidated/javascript.json index ad662e91..a61409b5 100644 --- a/public/consolidated/javascript.json +++ b/public/consolidated/javascript.json @@ -15,6 +15,20 @@ "contributors": [], "code": "const flattenArray = (arr) => arr.flat(Infinity);\n\n// Usage:\nconst nestedArray = [1, [2, [3, [4]]]];\nconsole.log(flattenArray(nestedArray)); // Output: [1, 2, 3, 4]\n" }, + { + "title": "Partition Array", + "description": "Splits an array into two arrays based on a callback function.", + "author": "Swaraj-Singh-30", + "tags": [ + "javascript", + "array", + "partition", + "reduce", + "utility" + ], + "contributors": [], + "code": "const partition = (arr, callback) =>\n arr.reduce(\n ([pass, fail], elem) => (callback(elem) ? [[...pass, elem], fail] : [pass, [...fail, elem]]),\n [[], []]\n );\n\n// Usage:\nconst numbers = [1, 2, 3, 4, 5, 6];\nconst isEven = (n) => n % 2 === 0;\nconsole.log(partition(numbers, isEven)); // Output: [[2, 4, 6], [1, 3, 5]]\n" + }, { "title": "Remove Duplicates", "description": "Removes duplicate values from an array.", From 452f2a14bee3ec5f1220b59d009b8c58cde51460 Mon Sep 17 00:00:00 2001 From: Swaraj Singh <76990410+Swaraj-Singh-30@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:11:01 +0530 Subject: [PATCH 3/3] Update javascript.json --- public/consolidated/javascript.json | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/public/consolidated/javascript.json b/public/consolidated/javascript.json index a61409b5..1caca666 100644 --- a/public/consolidated/javascript.json +++ b/public/consolidated/javascript.json @@ -15,20 +15,6 @@ "contributors": [], "code": "const flattenArray = (arr) => arr.flat(Infinity);\n\n// Usage:\nconst nestedArray = [1, [2, [3, [4]]]];\nconsole.log(flattenArray(nestedArray)); // Output: [1, 2, 3, 4]\n" }, - { - "title": "Partition Array", - "description": "Splits an array into two arrays based on a callback function.", - "author": "Swaraj-Singh-30", - "tags": [ - "javascript", - "array", - "partition", - "reduce", - "utility" - ], - "contributors": [], - "code": "const partition = (arr, callback) =>\n arr.reduce(\n ([pass, fail], elem) => (callback(elem) ? [[...pass, elem], fail] : [pass, [...fail, elem]]),\n [[], []]\n );\n\n// Usage:\nconst numbers = [1, 2, 3, 4, 5, 6];\nconst isEven = (n) => n % 2 === 0;\nconsole.log(partition(numbers, isEven)); // Output: [[2, 4, 6], [1, 3, 5]]\n" - }, { "title": "Remove Duplicates", "description": "Removes duplicate values from an array.", @@ -1019,4 +1005,4 @@ } ] } -] \ No newline at end of file +]