From d760581e2db2b7e4c31283c89669eb01d2137f3a Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 7 Feb 2018 23:33:45 +0000 Subject: [PATCH] Travis build: 1591 --- README.md | 4 ++-- docs/index.html | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index edadf62e6..1931813b8 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ average(1, 2, 3); * [`join`](#join) * [`last`](#last) * [`longestItem`](#longestitem) -* [`mapObject`](#mapobject) +* [`mapObject`](#mapobject-) * [`maxN`](#maxn) * [`minN`](#minn) * [`nthElement`](#nthelement) @@ -1590,7 +1590,7 @@ longestItem([1, 2, 3], 'foobar'); // 'foobar'
[⬆ Back to top](#table-of-contents) -### mapObject +### mapObject ![advanced](/advanced.svg) Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value. diff --git a/docs/index.html b/docs/index.html index 5011e05cf..1282dc6f3 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);
@@ -271,7 +271,7 @@ Object.assig
 longestItem(...['a', 'ab', 'abc'], 'abcd'); // 'abcd'
 longestItem([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]); // [1, 2, 3, 4, 5]
 longestItem([1, 2, 3], 'foobar'); // 'foobar'
-

mapObject

Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.

Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new Array to store the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).

const mapObject = (arr, fn) =>
+

mapObjectadvanced

Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.

Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new Array to store the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).

const mapObject = (arr, fn) =>
   (a => (
     (a = [arr, arr.map(fn)]), a[0].reduce((acc, val, ind) => ((acc[val] = a[1][ind]), acc), {})
   ))();