Javascript以数组中的项目为目标

时间:2016-03-25 14:26:48

标签: javascript

我如何定位此数组中的“数据”?

var waterBarData = {
labels: ["January", "February", "March", "April", "May", "June"],
datasets: [
    {
        label: "My Second dataset",
        fillColor: "rgba(98,203,49,1.0)",
        strokeColor: "rgba(98,203,49,0.8)",
        highlightFill: "rgba(98,203,49,0.75)",
        highlightStroke: "rgba(98,203,49,1)",
        data: [28, 48, 40, 19, 86, 27]
    }
]};

我的猜测如下,但似乎没有效果

waterBarData.datasets[data]

1 个答案:

答案 0 :(得分:4)

对象中的

datasets是一个数组,因此您需要通过索引访问它:

waterBarData.datasets[0]['data'];

// or using dot notation
waterBarData.datasets[0].data;