Adapter
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); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
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);Promise.resolve([1, 2, 3]) .then(call('map', x => 2 * x)) .then(console.log); //[ 2, 4, 6 ] @@ -862,6 +862,22 @@ console.log< }, {});const myObj = { Name: 'Adam', sUrnAME: 'Smith' }; const myObjLower = lowercaseKeys(myObj); // {name: 'Adam', surname: 'Smith'}; +mapKeys
Creates an object with keys generated by running the provided function for each key and the same values as the provided object.
Use
Object.keys(obj)to iterate over the object's keys. UseArray.reduce()to create a new object with the same values and mapped keys usingfn.const mapKeys = (obj, fn) => + Object.keys(obj).reduce((acc, k) => { + acc[fn(obj[k], k, obj)] = obj[k]; + return acc; + }, {}); +mapKeys({ a: 1, b: 2 }, (val, key) => key + val); // { a1: 1, b2: 2 } +mapValues
Creates an object with the same keys as the provided object and values generated by running the provided function for each value.
Use
Object.keys(obj)to iterate over the object's keys. UseArray.reduce()to create a new object with the same keys and mapped values usingfn.const mapValues = (obj, fn) => + Object.keys(obj).reduce((acc, k) => { + acc[k] = fn(obj[k], k, obj); + return acc; + }, {}); +const users = { + fred: { user: 'fred', age: 40 }, + pebbles: { user: 'pebbles', age: 1 } +}; +mapValues(users, u => u.age); // { fred: 40, pebbles: 1 }objectFromPairs
Creates an object from the given key-value pairs.
Use
Array.reduce()to create and combine key-value pairs.const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {});objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}objectToPairs
Creates an array of key-value pair arrays from an object.
Use
Object.keys()andArray.map()to iterate over the object's keys and produce an array with key-value pairs.const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]); @@ -1200,6 +1216,7 @@ Logs: { + const newPost = { "userId": 1, "id": 1337, diff --git a/snippets/httpPost.md b/snippets/httpPost.md index 00f7933f0..26181cd30 100644 --- a/snippets/httpPost.md +++ b/snippets/httpPost.md @@ -35,6 +35,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { + const newPost = { "userId": 1, "id": 1337, diff --git a/snippets/mapKeys.md b/snippets/mapKeys.md index 2b50ddd84..fac78308d 100644 --- a/snippets/mapKeys.md +++ b/snippets/mapKeys.md @@ -7,9 +7,12 @@ Use `Array.reduce()` to create a new object with the same values and mapped keys ```js const mapKeys = (obj, fn) => - Object.keys(obj).reduce((acc,k) => {acc[fn(obj[k], k, obj)] = obj[k]; return acc;},{}); + Object.keys(obj).reduce((acc, k) => { + acc[fn(obj[k], k, obj)] = obj[k]; + return acc; + }, {}); ``` ```js -mapKeys({ 'a': 1, 'b': 2 }, (val, key) => key + val); // { a1: 1, b2: 2 } +mapKeys({ a: 1, b: 2 }, (val, key) => key + val); // { a1: 1, b2: 2 } ``` diff --git a/snippets/mapValues.md b/snippets/mapValues.md index 79dbc7415..bb0fb187d 100644 --- a/snippets/mapValues.md +++ b/snippets/mapValues.md @@ -7,13 +7,16 @@ Use `Array.reduce()` to create a new object with the same keys and mapped values ```js const mapValues = (obj, fn) => - Object.keys(obj).reduce((acc,k) => {acc[k] = fn(obj[k], k, obj); return acc;},{}); + Object.keys(obj).reduce((acc, k) => { + acc[k] = fn(obj[k], k, obj); + return acc; + }, {}); ``` ```js const users = { - 'fred': { 'user': 'fred', 'age': 40 }, - 'pebbles': { 'user': 'pebbles', 'age': 1 } + fred: { user: 'fred', age: 40 }, + pebbles: { user: 'pebbles', age: 1 } }; mapValues(users, u => u.age); // { fred: 40, pebbles: 1 } ```