Created
August 23, 2016 06:08
-
-
Save stefanfrede/7f1344d93b1c44ed6f3e77e4a127ce85 to your computer and use it in GitHub Desktop.
Working with arrays that represent trees.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const deepMapWith = (fn) => | |
| function innerdeepMapWith (tree) { | |
| return Array.prototype.map.call(tree, (element) => | |
| Array.isArray(element) | |
| ? innerdeepMapWith(element) | |
| : fn(element) | |
| ); | |
| } | |
| /** | |
| * Example | |
| */ | |
| const report = | |
| [ [ { price: 1.99, id: 1 }, | |
| { price: 4.99, id: 2 }, | |
| { price: 7.99, id: 3 }, | |
| { price: 1.99, id: 4 }, | |
| { price: 2.99, id: 5 }, | |
| { price: 6.99, id: 6 } ], | |
| [ { price: 5.99, id: 21 }, | |
| { price: 1.99, id: 22 }, | |
| { price: 1.99, id: 23 }, | |
| { price: 1.99, id: 24 }, | |
| { price: 5.99, id: 25 } ], | |
| // ... | |
| [ { price: 7.99, id: 221 }, | |
| { price: 4.99, id: 222 }, | |
| { price: 7.99, id: 223 }, | |
| { price: 10.99, id: 224 }, | |
| { price: 9.99, id: 225 }, | |
| { price: 9.99, id: 226 } ] ]; | |
| deepMapWith(getWith('price'))(report) | |
| //=> [ [ 1.99, | |
| 4.99, | |
| 7.99, | |
| 1.99, | |
| 2.99, | |
| 6.99 ], | |
| [ 5.99, | |
| 1.99, | |
| 1.99, | |
| 1.99, | |
| 5.99 ], | |
| // ... | |
| [ 7.99, | |
| 4.99, | |
| 7.99, | |
| 10.99, | |
| 9.99, | |
| 9.99 ] ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment