我有一个对象,我正在将数据存储为

{"anti-social-behaviour":43,"burglary":24,"other-theft":29,"shoplifting":2,"vehicle-crime":27,"violent-crime":34,"criminal-damage-arson":17,"public-order":2,"drugs":1,"robbery":3,"other-crime":3,"bicycle-theft":1}


我正在尝试将这些数据解释为图表,Google API。使用以下

data.addRows([
  ['Drugs', crimes.drugs],
['bicycle-theft', crimes.bicycle-theft],
]);


Crimes.drugs效果很好,但Crimes.bicycle-盗窃正在引发

Uncaught (in promise) ReferenceError: theft is not defined
at drawChart (crimes.js:126)
at <anonymous>

最佳答案

由于属性bicycle-theft具有-,因此您无法通过dot operator访问,请尝试这种方式,

data.addRows([
  ['Drugs', crimes.drugs],
  ['bicycle-theft', crimes['bicycle-theft']],
]);

关于javascript - 未定义Javascript x对象/数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46846846/

10-09 20:34