1

I have the object:

var votes = {
  "Alex": {president: "Bob"},
  "Bob": {president: "Mary"},
  "Cindy": {president: "Cindy"},
  "Devin": {president: "Louise"},
  "Ernest": {president: "Fred"},
  "Fred": {president: "Louise"},
  "Gail": {president: "Fred"},
  "Hermann": {president: "Ivy"},
  "Ivy": {president: "Louise"},
  "John": {president: "Louise"},
  "Kerry": {president: "Fred"},
  "Louise": {president: "Nate"},
}

How would I go about counting all the votes for President? Specifically, how can I show that "Louis" had the most votes?

The only thing I have so far is being about to see who voted for who with:

for (i in votes)
{
     console.log(votes[i].president);
};
1
  • Thanks everyone for the help. I was able to use what is stated below and got it! Thanks again. Commented Dec 18, 2013 at 20:54

6 Answers 6

3
var president = [];

for(k in votes) {
    president.push(votes[k]['president']);
}

After this, you need to count the occurrence of each element in the president array.

Sign up to request clarification or add additional context in comments.

Comments

3

I worked this out. A little haphazard, but gives correct answer.

var votes = {
 "Alex": {president: "Bob"},
 "Bob": {president: "Mary"},
 "Cindy": {president: "Cindy"},
 "Devin": {president: "Louise"},
 "Ernest": {president: "Fred"},
 "Fred": {president: "Louise"},
 "Gail": {president: "Fred"},
 "Hermann": {president: "Ivy"},
 "Ivy": {president: "Louise"},
 "John": {president: "Louise"},
 "Kerry": {president: "Fred"},
 "Louise": {president: "Nate"}
  }
  var holder = {};
  var largest = 'Alex';
  for( var i in votes){
    var num = holder[votes[i].president] || 0;
    holder[votes[i].president] = 1 + num;
        if(holder[votes[largest].president] < holder[votes[i].president]){
            largest = votes[i].president;
        }
  }

  console.log(largest); // louise

Comments

1

You will have to make a list of every president and count in an array. As karthikr already mentioned in a comment

var presidents = {},
    president;
for(person in votes)
{
    president = votes[person].president;
    if (! (president in presidents))
        presidents[president] = [];
    presidents[president].push(person);
}

that will put every president in a list (presidents) with the people that voted for him.

to get the president with the highest ammount of votes is nothing more than iterating through the array and doing a check if this president has more votes than the current voted highest president.

var highest = false;
for(president in presidents)
{
    if (! highest || presidents[president].length > presidents[highest].length)
        highest = president;
}

Comments

1

That's a good start, now you'll have to code the loop to count the votes. Something like:

scores = {}; //will hold the names and votes.
for (i in votes)
{
     var votedfor = votes[i].president; // get who was voted for
     if(scores[votefor]) scores[votefor]++; //if we have the name in our scores, add 1
     else scores[votefor] = 1; //if we don't - set his(or her) score to 1 (which also add to scores).
};

Scores then holds {"Bob" : 1, "Fred" : 2, "Mary" : 1, "Louise" : 3 .... etc } Notice that it's unordered.

Comments

0

Something like this:

results = {}
for (voter in votes) {
    choice = votes[voter].president;
    if (!results[choice]) {
        results[choice] = 1 
    } else {
        results[choice]++
    }
}

Comments

0

Your best bet would be to do a map reduce using a framework like Backbone.js

Assuming you're doing this with no frameworks, you already have how to get who each person voted for. Why not store the totals in an array?

var voteTotals = [];
for (i in votes)
{
     // get who the current vote was for
     var currentVote = votes[i].president;
     // initialize to 0 votes if no votes found for this person yet
     if (!(currentVote in voteTotals)) voteTotals[currentVote] = 0;
     voteTotals[currentVote]++;
};

for (i in voteTotals)
{
    console.log(i + ": " + voteTotals[i]);
};

Then, you should be able to sort and get the values as needed!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.