Skip to content

Instantly share code, notes, and snippets.

@METACEO
Created May 4, 2016 07:49
Show Gist options
  • Save METACEO/14c69decb621c54b60c026f0b6a6bf0a to your computer and use it in GitHub Desktop.
Save METACEO/14c69decb621c54b60c026f0b6a6bf0a to your computer and use it in GitHub Desktop.
Array2Object: strictly return an object from an array of pairs.
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;
}
// 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!" }
@tunnckoCore
Copy link

Looks good. There is one more https://www.npmjs.com/package/arrays-to-object :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment