Add invertKeyValues
Inverts the key-value pairs of an object
This commit is contained in:
13
snippets/invertKeyValues.md
Normal file
13
snippets/invertKeyValues.md
Normal file
@ -0,0 +1,13 @@
|
||||
### 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.
|
||||
|
||||
```js
|
||||
const invertKeyValues = obj => Object.keys(obj).reduce((acc,key) => { acc[obj[key]] = key; return acc;},{});
|
||||
```
|
||||
|
||||
```js
|
||||
invertKeyValues({name:'John', age: 20}) // { 20: 'age', John: 'name' }
|
||||
```
|
||||
@ -65,6 +65,7 @@ initializeArrayWithRange:array
|
||||
initializeArrayWithValues:array
|
||||
inRange:math
|
||||
intersection:array
|
||||
invertKeyValues:object
|
||||
isAbsoluteURL:string
|
||||
isArmstrongNumber:math
|
||||
isArray:utility
|
||||
|
||||
Reference in New Issue
Block a user