diff --git a/README.md b/README.md index ec2ea9f30..87b3253ec 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,7 @@ average(1, 2, 3); * [`unzip`](#unzip) * [`unzipWith`](#unzipwith-) * [`without`](#without) +* [`xProd`](#xprod) * [`zip`](#zip) * [`zipObject`](#zipobject) * [`zipWith`](#zipwith-) @@ -2226,6 +2227,28 @@ without([2, 1, 2, 3], 1, 2); // [3]
[⬆ Back to top](#table-of-contents) +### 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. + +```js +const xProd = (a, b) => a.map(x => b.map(y => [x, y])); +``` + +
+Examples + +```js +xProd([1, 2], ['a', 'b']); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### zip Creates an array of elements, grouped based on the position in the original arrays. diff --git a/docs/index.html b/docs/index.html index 1a7e18bfc..39a9ae8a6 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -419,6 +419,8 @@ 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([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));
   return Array.from({ length: maxLength }).map((_, i) => {