javascript - Parse string into object -


i data database, when try parse json parse error accrue indicating not valid json format. (because values not in quotation).

i can not make changes data valuable & prefer not use replace if possible!

var data = "a,b,c"; data = json.parse('['+ data +']'); //error because there no quotation marks 

is there other javascript function can used parse value of data json or array.

as said in comments, data has not json format, don't try parse json.

instead, seems represents comma-separated list of values. obtain array these values can use string.prototype.split.

and then, wrap each item in object, can use array.prototype.map:

"a,b,c".split(',').map(function(item) {   return {0: item}; }); 

simplifying es6 arrow functions,

"a,b,c".split(',').map(i => ({0:i})); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -