Merge pull request #13 from albertorestifo/patch-1
Use Array.reduce() in creation of object from key-value pairs
This commit is contained in:
@ -280,11 +280,10 @@ const timeTaken = (f,...args) => {
|
|||||||
|
|
||||||
### Object from key-value pairs
|
### Object from key-value pairs
|
||||||
|
|
||||||
Use `map()` to create objects for each key-value pair, combine with `Object.assign()`.
|
Use `Array.reduce()` to create and combine key-value pairs.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const objectFromPairs = arr =>
|
const objectFromPairs = arr => arr => arr.reduce((a,b) => { a[b[0]] = b[1]; return a;}, {});
|
||||||
Object.assign(...arr.map( v => {return {[v[0]] : v[1]};} ));
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Powerset
|
### Powerset
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
### Object from key-value pairs
|
### Object from key-value pairs
|
||||||
|
|
||||||
Use `map()` to create objects for each key-value pair, combine with `Object.assign()`.
|
Use `Array.reduce()` to create and combine key-value pairs.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const objectFromPairs = arr =>
|
const objectFromPairs = arr => arr => arr.reduce((a,b) => { a[b[0]] = b[1]; return a;}, {});
|
||||||
Object.assign(...arr.map( v => {return {[v[0]] : v[1]};} ));
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user