2π
β
The newlabels
is having wrong JSON string format. Let change '
with "
with escape.
This will work:
var newlabels= "[\"label1\", \"label2\", \"label3\", \"label4\"]";
myChart.data.labels = JSON.parse(newlabels);
The variable newdatasets
works with JSON.parse
because its elements are number.
2π
To be valid JSON, you need double-quotes. So the code below works.
Both examples work. You just need double quotes as part of the string itself.
var newlabels2= '["label1", "label2", "label3", "label4"]';
console.log(JSON.parse(newlabels2));
var newlabels3= "[\"label1\", \"label2\", \"label3\", \"label4\"]";
console.log(JSON.parse(newlabels3));
2π
JSON does not support single quotes, so this code:
var newlabels= "['label1', 'label2', 'label3', 'label4']";
Becomes this:
var newlabels= '["label1", "label2", "label3", "label4"]';
Source:stackexchange.com