Skip to content

Instantly share code, notes, and snippets.

@bingeboy
Last active December 17, 2015 23:49
Show Gist options
  • Save bingeboy/5692603 to your computer and use it in GitHub Desktop.
Save bingeboy/5692603 to your computer and use it in GitHub Desktop.
Merge Arrays Into Object Literal
var x = ["blue", "yellow", "red", "green", "brown", "grey", "gray", "orange"];
var y = ["james", "john", "robert", "michael", "william", "david", "richard", "wayne"];
function merge2array(a, b) {
var obj = {};
var l = a.length;
var i;
for (i = 0; i < l; i++) {
obj[a[i]] = b[i];
}
return obj;
}
var obj = merge2array(x, y);
console.log(obj);
@bingeboy
Copy link
Author

bingeboy commented Jun 2, 2013

Also remember to wrap the object in {} for rest api.

Example: var wrapper:{"title here": obj}

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