diff --git a/README.md b/README.md
index dfbf147c2..fc9d10648 100644
--- a/README.md
+++ b/README.md
@@ -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 =>
Examples
```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' ] }
```
diff --git a/docs/index.html b/docs/index.html
index ff75776a2..90e378a0e 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -50,7 +50,7 @@
scrollToTop();
}
}, false);
- }
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 obj ={ selector: { to: { val:'val to select'} }, target: [1,2, { a:'test'}] };get(obj,'selector.to.val','target[0]','target[2].a');// ['val to select', 1, 'test']
-
invertKeyValues
Inverts the key-value pairs of an object, without mutating it.
Use Object.keys() and Array.reduce() to invert the key-value pairs of an object.
constinvertKeyValues= obj =>
+
invertKeyValues
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 and apply the function provided (if any). Omit the second argument, fn, to get the inverted keys without applying a function to them.
Creates a new object from the specified object, where all the keys are in lowercase.
Use Object.keys() and Array.reduce() to create a new object from the specified object. Convert each key in the original object to lowercase, using String.toLowerCase().