我有JSON
这样的结果:
[{"Januari":"0","Februari":"0","Maret":"0","April":"0","Mei":"7","Juni":"0","Juli":"0","Agustus":"0","September":"0","Oktober":"0","November":"0","Desember":"0"}]
如何将JSON
的值插入JavaScript
对象中?
var ctx = document.getElementById("myAreaChart");
var myLineChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: "Visitor",
data: [0,0,0,0,7,0,0,0,0,0,0,0], //<-- insert values here
}],
},
答案 0 :(得分:0)
使用Object.values
和Array.map
可以提取值并将其解析为int
,如下所示:
var json = [{"Januari":"0","Februari":"0","Maret":"0","April":"0","Mei":"7","Juni":"0","Juli":"0","Agustus":"0","September":"0","Oktober":"0","November":"0","Desember":"0"}];
var numbers = Object.values(json[0]).map(s => parseInt(s, 10));
最后更改您的示例:
data: numbers,