Last active
November 4, 2019 18:32
-
-
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
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
/** | |
* 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