Reputation: 7838
I have this array:
var objectArray = [{url:"www.google.com", id: "google"},
{url:"www.apple.com", id: "apple"},
{url:"www.facebook.com", id: "facebook"}];
Is it possible to convert to a JavaScript object that is formed like this:
var newObject = {"google": "www.google.com",
"apple": "www.apple.com",
"facebook": "www.facebook.com"};
Upvotes: 1
Views: 57
Reputation: 33409
You can manually loop over the array and convert it.
var obj = {};
for (var i = 0; i < objectArray.length; i++) {
obj[objectArray[i].id] = objectArray[i].url;
}
Upvotes: 4