Skip to content

Instantly share code, notes, and snippets.

@leo-bianchi
Last active November 4, 2019 18:32
Show Gist options
  • Save leo-bianchi/6c9f3d6e4c57923ea8b0b5b7dffd7d0f to your computer and use it in GitHub Desktop.
Save leo-bianchi/6c9f3d6e4c57923ea8b0b5b7dffd7d0f to your computer and use it in GitHub Desktop.
Constructs an object from a array, using even indexes as key and odd indexes as value
/**
* Constructs an object from a array, using even indexes as key and odd indixes as value
*
* @param {Array} myArray - Array to be transformed
* @returns {Object}
*/
function toObject(myArray) {
let r = {};
for (let i = 0; i < myArray.length; i += 2) {
let key = (myArray[i]),
value = myArray[i + 1];
r[key] = value;
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment