How I can convert array to object in Javascript

mo.dhouibi

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

adeneo

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.

edited at
0

Comments

0 comments
Login to comment

Related

how can i convert object into an array in javascript?

How can I convert the "arguments" object to an array in JavaScript?

How can I convert the object array to GraphQL format in Javascript?

How can I convert this object of objects into an array of objects?

How can I convert a Request object into a stringifiable object in JavaScript?

How can I convert this object to an array and access values by key in React?

How can I convert an array to an object by splitting strings?

How can I convert to Json Object to Json Array in Karate?

How can I parse this as an object or array with javascript?

How can I convert a string to object in Javascript?

How can I convert my state from an object into a mappable array?

How can I convert IList<object> to string array?

How can I retrieve the object into an array in Javascript?

How can i convert array to SimpleXMLElement Object

How do I convert an array into an object within array - Angular and Javascript

How can I convert a jQuery object to a JS array?

JS How can I convert a serialized object to an Array?

How can I convert string to array in JavaScript?

How can I convert this list of pairs into an object that contains an array of arrays?

How Can I Convert a Byte Array into A User-Defined Object?

How i can convert array of object into array then a unique count of array

How i can to convert array to object of array in react?

How can I convert this object into an Array using GAS?

how can i change array of object to object of array in javascript?

How can I reverse an array of object in Javascript?

How can i convert an array in Json string to c# object?

How can I convert an array of objects to array with a unique id in javascript?

How can I convert an array of strings to an object using jq?

How to convert an object which can be array or single object to an array in javascript