Q:

Using JavaScript Object values like an array

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.

A:

You cannot access object properties that way. You have to know what the names of the indices are; that's basically how they work. I imagine you could do what you want however, by iterating over the properties of the object. That way you don't need to know beforehand what the names of the properties are:
var rowData = [];

for(var property in daten[item]) {
    var value = daten[item][property];

    //Since you want to convert some strings to integers, this
    //regex checks to see if the value is an integer or not
    if(/^-?\d+$/.test(value)) {
        rowData.push(parseInt(value, 10))   
    } else {
        rowData.push(value);
    }
}

data.addRow(rowData);

Note: Keep in mind that this doesn't give you a predictable order for the keys in daten[item].

