Travis build: 1337

This commit is contained in:
30secondsofcode
2018-01-23 15:34:14 +00:00
parent fefea87775
commit d47f85acc0
2 changed files with 16 additions and 9 deletions

View File

@ -4354,14 +4354,17 @@ get(obj, 'selector.to.val', 'target[0]', 'target[2].a'); // ['val to select', 1,
### invertKeyValues
Inverts the key-value pairs of an object, without mutating it.
Inverts the key-value pairs of an object, without mutating it. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. If a function is supplied, it is applied to each inverted key.
Use `Object.keys()` and `Array.reduce()` to invert the key-value pairs of an object.
Use `Object.keys()` and `Array.reduce()` to invert the key-value pairs of an object and apply the function provided (if any).
Omit the second argument, `fn`, to get the inverted keys without applying a function to them.
```js
const invertKeyValues = obj =>
const invertKeyValues = (obj, fn) =>
Object.keys(obj).reduce((acc, key) => {
acc[obj[key]] = key;
const val = fn ? fn(obj[key]) : obj[key];
acc[val] = acc[val] || [];
acc[val].push(key);
return acc;
}, {});
```
@ -4370,7 +4373,8 @@ const invertKeyValues = obj =>
<summary>Examples</summary>
```js
invertKeyValues({ name: 'John', age: 20 }); // { 20: 'age', John: 'name' }
invertKeyValues({ a: 1, b: 2, c: 1 }); // { 1: [ 'a', 'c' ], 2: [ 'b' ] }
invertKeyValues({ a: 1, b: 2, c: 1 }, value => 'group' + value); // { group1: [ 'a', 'c' ], group2: [ 'b' ] }
```
</details>

File diff suppressed because one or more lines are too long