Update and rename sqaure_it.md to mapObject.md
This commit is contained in:
14
snippets/mapObject.md
Normal file
14
snippets/mapObject.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
### mapObject
|
||||||
|
|
||||||
|
Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.
|
||||||
|
|
||||||
|
Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new `Array` to stor the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).
|
||||||
|
|
||||||
|
```js
|
||||||
|
const mapObject = (arr, fn) =>
|
||||||
|
(a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( );
|
||||||
|
/*
|
||||||
|
const squareIt = arr => mapObject(arr, a => a*a)
|
||||||
|
squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 }
|
||||||
|
*/
|
||||||
|
```
|
||||||
@ -1,11 +0,0 @@
|
|||||||
### Square The Data
|
|
||||||
|
|
||||||
Using an anonymous inner function scope we declare an undefined memory space using closures for storing a return value. We then use a new Array to store the array with a map of the function over it's data set and a comma operator to return a second step without needing to move from one context to another thank to closures and order of operations.
|
|
||||||
|
|
||||||
Notice the need to declare an order of operations(parenthesis) around the anonymous inner function so we can call it immediately.
|
|
||||||
|
|
||||||
```js
|
|
||||||
const mapObject = (arr, fn) => (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( )
|
|
||||||
const squareIt = arr => mapObject(arr, a => a*a)
|
|
||||||
squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 }
|
|
||||||
```
|
|
||||||
Reference in New Issue
Block a user