I am trying to convert in Javascript an array
A=['"age":"20"','"name":"John"','"email":"[email protected]"'];
to object
O={"age":"20","name":"John","email":"[email protected]"}.
How I can do this. Thanks
Should be straight forward, just iterate and split on the colon
var A = ['"age":"20"','"name":"John"','"email":"[email protected]"'];
var O = {};
A.forEach(function(item) {
var parts = item.split(':').map(function(x) { return x.trim().replace(/\"/g,'') });
O[parts[0]] = parts[1];
});
document.body.innerHTML = '<pre>' + JSON.stringify(O, null, 4) + '</pre>';
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments