diff --git a/README.md b/README.md index 08fbe0fcd..75c988bff 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,8 @@ average(1, 2, 3); * [`cleanObj`](#cleanobj) * [`invertKeyValues`](#invertkeyvalues) * [`lowercaseKeys`](#lowercasekeys) +* [`mapKeys`](#mapkeys) +* [`mapValues`](#mapvalues) * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) * [`orderBy`](#orderby) @@ -3974,6 +3976,64 @@ const myObjLower = lowercaseKeys(myObj); // {name: 'Adam', surname: 'Smith'};
[⬆ Back to top](#table-of-contents) +### 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. +Use `Array.reduce()` to create a new object with the same values and mapped keys using `fn`. + +```js +const mapKeys = (obj, fn) => + Object.keys(obj).reduce((acc, k) => { + acc[fn(obj[k], k, obj)] = obj[k]; + return acc; + }, {}); +``` + +
+Examples + +```js +mapKeys({ a: 1, b: 2 }, (val, key) => key + val); // { a1: 1, b2: 2 } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### 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. +Use `Array.reduce()` to create a new object with the same keys and mapped values using `fn`. + +```js +const mapValues = (obj, fn) => + Object.keys(obj).reduce((acc, k) => { + acc[k] = fn(obj[k], k, obj); + return acc; + }, {}); +``` + +
+Examples + +```js +const users = { + fred: { user: 'fred', age: 40 }, + pebbles: { user: 'pebbles', age: 1 } +}; +mapValues(users, u => u.age); // { fred: 40, pebbles: 1 } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### objectFromPairs Creates an object from the given key-value pairs. @@ -5366,6 +5426,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { + const newPost = { "userId": 1, "id": 1337, diff --git a/docs/index.html b/docs/index.html index 2979291a1..657d6969b 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

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);
+      }

logo 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. Use Array.reduce() to create a new object with the same values and mapped keys using fn.

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. Use Array.reduce() to create a new object with the same keys and mapped values using fn.

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() and Array.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 }
 ```