Created
May 4, 2016 07:49
-
-
Save METACEO/14c69decb621c54b60c026f0b6a6bf0a to your computer and use it in GitHub Desktop.
Array2Object: strictly return an object from an array of pairs.
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 Array2Object(array){ | |
var object = {}, len = array.length; | |
if(len % 2 !== 0) return object; | |
for(var index = 0; index < len; index++) object[array[index]] = array[++index]; | |
return 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
// minified version: | |
function Array2Object(a){var b=a.length,c={},d=0;if(b%2!==0)return c;for(;d<b;d++)c[a[d]]=a[++d];return c} | |
var someArray = [ | |
"array", [123,"abc"], | |
"boolean", true, | |
"function", Array2Object, | |
"number", 123, | |
"object", {"123":"abc"}, | |
"string", "Hello world!" | |
]; | |
Array2Object(someArray); | |
// { array: Array[2], boolean: true, function: Array2Object(), number: 123, object: Object, string: "Hello world!" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks good. There is one more https://www.npmjs.com/package/arrays-to-object :)