I'm fairly new to the JavaScript world, and I am not entirely sure how to use an object as if it was an array (accessing the information from within the Object via indexes, rather than specific names).
I would like to retrieve the values of an object's content based on it's index, similar to that of an array. This is illustrated in my code below:
function pieChart(daten, width, height, div)
{
var data = new google.visualization.DataTable();
data.addColumn('string');
data.addColumn('number');
//returns: [Object, Object, Object, Object]
console.log(daten);
for(item in daten)
{
console.log(daten[item]);
//The following 4 lines are the output of the console log
//Object {crimes: "300", location: "Cardiff"}
//Object {crimes: "900", location: "London"}
//Object {crimes: "500", location: "Manchester"}
//Object {crimes: "400", location: "Dublin"}
//here in lies the problem...
data.addRow([daten[item].location, parseInt(daten[item].crimes)]);
//the output would be: ["Dublin", 400] etc...
}
var chart = new google.visualization.pieChart(document.getElementById(div));
chart.draw(data, {"width": width, "height": height});
}
Essentially, I want to be able to do data.addRow([daten[item][0], daten[item[1]])
as I will not know location and crimes at runtime.
Edit:
I should note that I using Google Visualisations API (as illustrated above), which takes an array as a value for data.addRow()
. As such, a possible solution would be to convert the object to an array with the specified requirements above, such that the output is: ["Dublin", 400]["London", 900]["Manchester", 500]["Cardiff", 300]
. However, I am not sure how to go about doing this.