Travis build: 1194
This commit is contained in:
61
README.md
61
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'};
|
||||
<br>[⬆ 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;
|
||||
}, {});
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
mapKeys({ a: 1, b: 2 }, (val, key) => key + val); // { a1: 1, b2: 2 }
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ 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;
|
||||
}, {});
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
const users = {
|
||||
fred: { user: 'fred', age: 40 },
|
||||
pebbles: { user: 'pebbles', age: 1 }
|
||||
};
|
||||
mapValues(users, u => u.age); // { fred: 40, pebbles: 1 }
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ 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,
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -35,6 +35,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
|
||||
|
||||
|
||||
|
||||
|
||||
const newPost = {
|
||||
"userId": 1,
|
||||
"id": 1337,
|
||||
|
||||
@ -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 }
|
||||
```
|
||||
|
||||
@ -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 }
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user