diff --git a/README.md b/README.md index 7bf7f2801..d01778d8b 100644 --- a/README.md +++ b/README.md @@ -2246,10 +2246,10 @@ without([2, 1, 2, 3], 1, 2); // [3] Creates a new array out of the two supplied by creating each possible pair from the arrays. -Use `Array.map()` to produce every possible pair from the elements of the two arrays. +Use `Array.reduce()`, `Array.map()` and `Array.concat()` to produce every possible pair from the elements of the two arrays and save them in an array. ```js -const xProd = (a, b) => a.map(x => b.map(y => [x, y])); +const xProd = (a, b) => a.reduce((acc, x) => acc.concat(b.map(y => [x, y])), []); ```
diff --git a/docs/index.html b/docs/index.html index 50aa785be..d0b48334e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -422,7 +422,7 @@ Object.assig
unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)); // [3, 30, 300]
 

without

Filters out the elements of an array, that have one of the specified values.

Use Array.filter() to create an array excluding(using !Array.includes()) all given values.

(For a snippet that mutates the original array see pull)

const without = (arr, ...args) => arr.filter(v => !args.includes(v));
 
without([2, 1, 2, 3], 1, 2); // [3]
-

xProd

Creates a new array out of the two supplied by creating each possible pair from the arrays.

Use Array.map() to produce every possible pair from the elements of the two arrays.

const xProd = (a, b) => a.map(x => b.map(y => [x, y]));
+

xProd

Creates a new array out of the two supplied by creating each possible pair from the arrays.

Use Array.reduce(), Array.map() and Array.concat() to produce every possible pair from the elements of the two arrays and save them in an array.

const xProd = (a, b) => a.reduce((acc, x) => acc.concat(b.map(y => [x, y])), []);
 
xProd([1, 2], ['a', 'b']); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
 

zip

Creates an array of elements, grouped based on the position in the original arrays.

Use Math.max.apply() to get the longest array in the arguments. Creates an array with that length as return value and use Array.from() with a map-function to create an array of grouped elements. If lengths of the argument-arrays vary, undefined is used where no value could be found.

const zip = (...arrays) => {
   const maxLength = Math.max(...arrays.map(x => x.length));
diff --git a/snippets/xProd.md b/snippets/xProd.md
index d5ff74b61..df1bd897a 100644
--- a/snippets/xProd.md
+++ b/snippets/xProd.md
@@ -5,7 +5,7 @@ Creates a new array out of the two supplied by creating each possible pair from
 Use `Array.reduce()`, `Array.map()` and `Array.concat()` to produce every possible pair from the elements of the two arrays and save them in an array.
 
 ```js
-const xProd = (a, b) => a.reduce((acc,x) => acc.concat(b.map(y => [x, y])),[]);
+const xProd = (a, b) => a.reduce((acc, x) => acc.concat(b.map(y => [x, y])), []);
 ```
 
 ```js