Created
February 12, 2015 07:04
-
-
Save liammclennan/8c642478f6e39c1303d2 to your computer and use it in GitHub Desktop.
Mapping serialized tuples (F#) to JavaScript objects
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
| // response from server | |
| // "[{"item1":{"id":"ce957038-fed0-4c09-8594-5475efcac4d3","name":"https://github.com/liammcl..."},"item2":3}, ...]" | |
| doSomethingAjaxing().then(mapSerializedTuples.bind(null, ['deck', 'count'])); | |
| // after mapping | |
| // "[{"deck":{"id":"ce957038-fed0-4c09-8594-5475efcac4d3","name":"https://github.com/liammcl..."},"count":3}, ...]" | |
| function mapSerializedTuple(keyNames) { | |
| return function (tuple) { | |
| var o = {}; | |
| keyNames.forEach(function (key,i) { | |
| o[key] = tuple['item' + (i+1)]; | |
| }); | |
| return o; | |
| }; | |
| } | |
| function mapSerializedTuples(keyNames, tuples) { | |
| return tuples.map(mapSerializedTuple(keyNames)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment