zosh
zosh

Reputation: 83

Convert a string made from array toString() back to Array

I converted an array to a string and added it to a TextArea. The user edited the TextArea and I need to now update the array by calling in the same string of data I first produced. How would you do this?

The string which I have produced and need to convert back to an array is:

{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30} 

I tried to use the JSON Parser JSON.parse(text)

Upvotes: 2

Views: 979

Answers (3)

Aziz.G
Aziz.G

Reputation: 3721

Format your string:

const text = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}'

console.log(JSON.parse(`[ ${text}]`))

Upvotes: 2

Vijesh
Vijesh

Reputation: 815

The Below code should work:

var text_string = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}';
console.log(JSON.parse(`[${text_string}]`));

Upvotes: 1

Mike Cluck
Mike Cluck

Reputation: 32521

You just need to format your string as an array in JSON format. You can do that like so:

JSON.parse('[' + text + ']')

Upvotes: 1

Related Questions