Reputation: 83
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
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
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
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