-1

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.

Community
  • 1
  • 1
Jay Rainey
  • 386
  • 6
  • 22
  • Objects are not ordered. You can't reliably reference them by index. –  Apr 26 '13 at 15:38
  • Essentially, you want to iterate through the properties of `daten[item]` right? Have you tried this? http://stackoverflow.com/questions/684672/loop-through-javascript-object – Hudon Apr 26 '13 at 15:39
  • How are you going to determine the proper order of the arguments to `.addRow()`? Or does the order matter? –  Apr 26 '13 at 15:43

3 Answers3

1

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].

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • What exactly does the regex `/-?^\d+$/` do? – Jay Rainey Apr 26 '13 at 15:43
  • @JayRainey I've updated the answer, but basically it checks to see if the string contains integers or not (even negative ones). If so, it will call `parseInt` on that value. – Vivin Paliath Apr 26 '13 at 15:44
  • For me, I got an error saying item was undefined. To fix that, I used another for loop, and it worked as yours should have. – Jay Rainey Apr 26 '13 at 16:03
  • Ah yes, you do need another loop outside the one I have here. I just showed you how to access each property individually by iterating over the object's properties. – Vivin Paliath Apr 26 '13 at 16:04
0

You should access the values of the object by their keys:

data.addRow(item.location, item.crimes);
tilleryj
  • 14,259
  • 3
  • 23
  • 22
  • 2
    This doesn't answer the OP's question. – Vivin Paliath Apr 26 '13 at 15:39
  • This would work, but I do not know the values at runtime. Location and crimes could by anything, and that's essentially what i'm looking for. Something that allows me to do `item.property1`, `item.property2`. – Jay Rainey Apr 26 '13 at 15:42
0

you can access the value by this waydaten[item][location]

clark
  • 11