0

I'm trying to print a value stored in a JS object but I don't know how can I access it. Here's my code:

var the_votes = {
    "Heredia": {
        voters: 70000,
        parties: {
            pln: 0,
            pac: 0,
            plib: 0,
            rc: 0
        }
    },
    "San Jose": {
        voters: 200000,
        parties: {
            pln: 0,
            pac: 0,
            plib: 0,
            rc: 0
        }
    },
    "Alajuela": {
        voters: 80000,
        parties: {
            pln: 0,
            pac: 0,
            plib: 0,
            rc: 0
        }
    },
    "Cartago": {
        voters: 50000,
        parties: {
            pln: 0,
            pac: 0,
            plib: 0,
            rc: 0
        }
    },
    "Puntarenas": {
        voters: 100000,
        parties: {
            pln: 0,
            pac: 0,
            plib: 0,
            rc: 0
        }
    },
    "Limon": {
        voters: 60000,
        parties: {
            pln: 0,
            pac: 0,
            plib: 0,
            rc: 0
        }
    },
    "Guanacaste": {
        voters: 90000,
        parties: {
            pln: 0,
            pac: 0,
            plib: 0,
            rc: 0
        }
    }

};

And I want to print the value "voters" on the console with this method:

function updateTable(votes) {
    table_clear();
    var table = $("#elections");
    var tbody = table.append($("<tbody/>"));
    $.each(votes, function (province, data) {
        var row = $("<tr/>");
        row.append($("<td/>")).html(province);
        $.each(data.parties, function (partyName, partyValue) {
            var td = $("<td/>");
            td.html(partyValue);
            row.append(td);
            td = $("<td/>");
            td.html();
            console.log(province.voters);
            row.append(td);

        });

        tbody.append(row);
    });

};

I keep getting an "undefined" on this line: "console.log(province.voters);

How can I access the value under this method structure?

j08691
  • 204,283
  • 31
  • 260
  • 272
Jorge Rodríguez
  • 347
  • 2
  • 5
  • 17
  • 1
    `voters` and `parties` are properties of the same object. Why then are you accessing `data.parties` but `province.voters`? How about `data.voters`? – Felix Kling Sep 19 '15 at 20:03
  • it probably is undefined... try using this to make sure and find what u need: http://stackoverflow.com/questions/5357442/how-to-inspect-javascript-objects – yael alfasi Sep 19 '15 at 20:06
  • @yaelalfasi: Yes. Try `console.log("Heredia".voters)`. – Felix Kling Sep 19 '15 at 20:06

4 Answers4

0

you want to console.log(data.voters);

province is simply the string identifier such as "Puntarenas"

alternatively you could do console.log(votes[province].voters);

David Furlong
  • 1,343
  • 1
  • 12
  • 15
0

province is a string. It doesn't have a property voters.

data is the object you want to access (which you already do correctly for data.parties):

console.log(data.voters);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

To print voters for every province:

console.log(data.voters);
0
console.log(the_votes.Heredia.voters)

This will print voters in "Heredia"

zlatan
  • 3,346
  • 2
  • 17
  • 35