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
datavaluable & prefer not usereplaceif 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
Post a Comment