Last active
August 1, 2017 10:37
-
-
Save pentaphobe/dc575d46789eae3173fc5afcebf19507 to your computer and use it in GitHub Desktop.
ZIP interleaved array into object
This file contains 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
function zipInterleaved(arr) { | |
return arr.reduce( (prev, cur, idx) => { | |
if (idx % 2 == 0) { | |
return {obj:prev, key:cur}; | |
} else { | |
let obj = prev.obj; | |
obj[prev.key] = cur; | |
return prev.obj; | |
} | |
}, {}); | |
} | |
/** | |
* usage: zipInterleaved(['foo', 'bar', 'magic', 'custard']) | |
* result: { foo: 'bar', magic: 'custard' } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment