Add invertKeyValues

Inverts the key-value pairs of an object
This commit is contained in:
Angelos Chalaris
2018-01-01 17:33:46 +02:00
parent cba58d63f5
commit 95d0df48fb
2 changed files with 14 additions and 0 deletions

View 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' }
```

View File

@ -65,6 +65,7 @@ initializeArrayWithRange:array
initializeArrayWithValues:array initializeArrayWithValues:array
inRange:math inRange:math
intersection:array intersection:array
invertKeyValues:object
isAbsoluteURL:string isAbsoluteURL:string
isArmstrongNumber:math isArmstrongNumber:math
isArray:utility isArray:utility