From 565c5e6a66aad71fab0b2ae0143a2c2e7c63741b Mon Sep 17 00:00:00 2001 From: taltmann42 Date: Thu, 14 Dec 2017 23:54:55 +0100 Subject: [PATCH 1/4] Array zip Zip method from https://lodash.com/docs/4.17.4#zip --- snippets/array-zip.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 snippets/array-zip.md diff --git a/snippets/array-zip.md b/snippets/array-zip.md new file mode 100644 index 000000000..d63f79b15 --- /dev/null +++ b/snippets/array-zip.md @@ -0,0 +1,17 @@ +### Array zip + +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. + +```js +const zip = (...arrays) => { + const maxLength = Math.max.apply(null, arrays.map(a => a.length)); + return Array.from({length: maxLength}).map((_, i) => { + return Array.from({length: arrays.length}, (_, k) => arrays[k][i]) + }) +} + +//zip(['a', 'b'], [1, 2], [true, false]); -> [['a', 1, true], ['b', 2, false]] +zip(['a'], [1, 2], [true, false]); -> [['a', 1, true], [undefined, 2, false]] +``` From a4c7673ec272ea5f7104ff303aeedee7c332be92 Mon Sep 17 00:00:00 2001 From: taltmann42 Date: Thu, 14 Dec 2017 23:56:51 +0100 Subject: [PATCH 2/4] Update array-zip.md --- snippets/array-zip.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/array-zip.md b/snippets/array-zip.md index d63f79b15..08c40ed68 100644 --- a/snippets/array-zip.md +++ b/snippets/array-zip.md @@ -13,5 +13,5 @@ const zip = (...arrays) => { } //zip(['a', 'b'], [1, 2], [true, false]); -> [['a', 1, true], ['b', 2, false]] -zip(['a'], [1, 2], [true, false]); -> [['a', 1, true], [undefined, 2, false]] +//zip(['a'], [1, 2], [true, false]); -> [['a', 1, true], [undefined, 2, false]] ``` From 699bda348de95f7b3aae660c5a3aa7a3216818b4 Mon Sep 17 00:00:00 2001 From: taltmann42 Date: Fri, 15 Dec 2017 00:23:10 +0100 Subject: [PATCH 3/4] Create array-without.md --- snippets/array-without.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 snippets/array-without.md diff --git a/snippets/array-without.md b/snippets/array-without.md new file mode 100644 index 000000000..353d1308e --- /dev/null +++ b/snippets/array-without.md @@ -0,0 +1,9 @@ +### Array without + +Filters out array elements if they are included in the given `values` arguments + +```js +const without = ((arr, ...values) => arr.filter(x => !values.includes(x))) + +//without([2, 1, 2, 3], 1, 2); -> [3] +``` From 88fc66efd657e28f30c990236852936fe36594d1 Mon Sep 17 00:00:00 2001 From: taltmann42 Date: Fri, 15 Dec 2017 00:30:18 +0100 Subject: [PATCH 4/4] Delete array-without.md --- snippets/array-without.md | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 snippets/array-without.md diff --git a/snippets/array-without.md b/snippets/array-without.md deleted file mode 100644 index 353d1308e..000000000 --- a/snippets/array-without.md +++ /dev/null @@ -1,9 +0,0 @@ -### Array without - -Filters out array elements if they are included in the given `values` arguments - -```js -const without = ((arr, ...values) => arr.filter(x => !values.includes(x))) - -//without([2, 1, 2, 3], 1, 2); -> [3] -```