Skip to content

Instantly share code, notes, and snippets.

@pentaphobe
Last active August 1, 2017 10:37
Show Gist options
  • Save pentaphobe/dc575d46789eae3173fc5afcebf19507 to your computer and use it in GitHub Desktop.
Save pentaphobe/dc575d46789eae3173fc5afcebf19507 to your computer and use it in GitHub Desktop.
ZIP interleaved array into object
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