0

Want to be able to add properties from an object in an object to a separate object, have got it working to a certain point but am getting NaN returned??

var votes = { "Alex": { president: "Bob", vicePresident: "Devin", secretary: "Gail", treasurer: "Kerry" },
"Bob": { president: "Mary", vicePresident: "Hermann", secretary:   "Fred", treasurer: "Ivy" },
"Cindy": { president: "Cindy", vicePresident: "Hermann", secretary: "Bob", treasurer: "Bob" },
"Devin": { president: "Louise", vicePresident: "John", secretary: "Bob", treasurer: "Fred" }, 
 "Ernest": { president: "Fred", vicePresident: "Hermann", secretary:  "Fred", treasurer: "Ivy" },
"Fred": { president: "Louise", vicePresident: "Alex", secretary: "Ivy", treasurer: "Ivy" },
"Gail": { president: "Fred", vicePresident: "Alex", secretary: "Ivy", treasurer: "Bob" },
"Hermann": { president: "Ivy", vicePresident: "Kerry", secretary: "Fred", treasurer: "Ivy" },
 "Ivy": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Gail" },
"John": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Kerry" },
"Kerry": { president: "Fred", vicePresident: "Mary", secretary: "Fred", treasurer: "Ivy" },
"Louise": { president: "Nate", vicePresident: "Alex", secretary: "Mary", treasurer: "Ivy" },
"Mary": { president: "Louise", vicePresident: "Oscar", secretary: "Nate", treasurer: "Ivy" },
"Nate": { president: "Oscar", vicePresident: "Hermann", secretary: "Fred", treasurer: "Tracy" },
"Oscar": { president: "Paulina", vicePresident: "Nate", secretary: "Fred", treasurer: "Ivy" },
"Paulina": { president: "Louise", vicePresident: "Bob", secretary: "Devin", treasurer: "Ivy" },
"Quintin": { president: "Fred", vicePresident: "Hermann", secretary: "Fred", treasurer: "Bob" },
"Romanda": { president: "Louise", vicePresident: "Steve", secretary: "Fred", treasurer: "Ivy" },
"Steve": { president: "Tracy", vicePresident: "Kerry", secretary: "Oscar", treasurer: "Xavier" },
"Tracy": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Ivy" },
"Ullyses": { president: "Louise", vicePresident: "Hermann", secretary: "Ivy", treasurer: "Bob" },
"Valorie": { president: "Wesley", vicePresident: "Bob", secretary: "Alex", treasurer: "Ivy" },
"Wesley": { president: "Bob", vicePresident: "Yvonne", secretary: "Valorie", treasurer: "Ivy" },
"Xavier": { president: "Steve", vicePresident: "Hermann", secretary: "Fred", treasurer: "Ivy" },
"Yvonne": { president: "Bob", vicePresident: "Zane", secretary: "Fred", treasurer: "Hermann" },
"Zane": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Mary" }
};

// Tally the votes in voteCount.
var voteCount = {
   president: {},
   vicePresident: {},
   secretary: {},
   treasurer: {}
};

for (var student in votes) {
  if (votes.hasOwnProperty(student)) {
    var office = votes[student];
    for (var prop in office) {
      if (!office.hasOwnProperty("president")) {
        voteCount["president"][office["president"]] = 0;
      } else {
        voteCount["president"][office["president"]] += 1;
      }
    }
  }
}

the above for in loop return NaN!!!
I am getting stuff across to voteCount but not numbers of votes??

voteCount becomes

voteCount
{ president: 
  { Bob: NaN,
    Mary: NaN,
    Cindy: NaN,
    Louise: NaN,
    Fred: NaN,
2
  • Are you sure you actually mean office.hasOwnProperty("president")? It seems to me you might want to use voteCount[prop].hasOwnProperty(office[prop]) instead. Commented Apr 23, 2015 at 2:57
  • Hmmm, tried that but it give 3 for each. Rather than the actual number of votes for each person / property. Commented Apr 23, 2015 at 3:08

3 Answers 3

1

I think your error was in the addition of properties to the result object.

This is a different approach, it builds the results object dynamically out of the votes by role.

var results = {};

var n,r,v;

for (n in votes) {
    for (r in votes[n]) {
        if (typeof results[r] === "undefined") {
             results[r] = {};
        }
        if (typeof results[r][votes[n][r]] === "undefined") {
             results[r][votes[n][r]] = 0;
        }
        results[r][votes[n][r]]+=1;
    }
}
console.log(results);

Also, it is better to put all the vars at the beginning of the code, placing them in the fors does not limit their scope, they are declared at parse time and will be global.

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

Comments

1

You could fix this by changing the line that checks if office hasOwnProperty of president:

 if (!office.hasOwnProperty("president")) {

to this:

 if (!voteCount["president"].hasOwnProperty(office["president"])) {

The end result is:

        var votes = { "Alex": { president: "Bob", vicePresident: "Devin", secretary: "Gail", treasurer: "Kerry" },
        "Bob": { president: "Mary", vicePresident: "Hermann", secretary:   "Fred", treasurer: "Ivy" },
        "Cindy": { president: "Cindy", vicePresident: "Hermann", secretary: "Bob", treasurer: "Bob" },
        "Devin": { president: "Louise", vicePresident: "John", secretary: "Bob", treasurer: "Fred" },
        "Ernest": { president: "Fred", vicePresident: "Hermann", secretary:  "Fred", treasurer: "Ivy" },
        "Fred": { president: "Louise", vicePresident: "Alex", secretary: "Ivy", treasurer: "Ivy" },
        "Gail": { president: "Fred", vicePresident: "Alex", secretary: "Ivy", treasurer: "Bob" },
        "Hermann": { president: "Ivy", vicePresident: "Kerry", secretary: "Fred", treasurer: "Ivy" },
        "Ivy": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Gail" },
        "John": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Kerry" },
        "Kerry": { president: "Fred", vicePresident: "Mary", secretary: "Fred", treasurer: "Ivy" },
        "Louise": { president: "Nate", vicePresident: "Alex", secretary: "Mary", treasurer: "Ivy" },
        "Mary": { president: "Louise", vicePresident: "Oscar", secretary: "Nate", treasurer: "Ivy" },
        "Nate": { president: "Oscar", vicePresident: "Hermann", secretary: "Fred", treasurer: "Tracy" },
        "Oscar": { president: "Paulina", vicePresident: "Nate", secretary: "Fred", treasurer: "Ivy" },
        "Paulina": { president: "Louise", vicePresident: "Bob", secretary: "Devin", treasurer: "Ivy" },
        "Quintin": { president: "Fred", vicePresident: "Hermann", secretary: "Fred", treasurer: "Bob" },
        "Romanda": { president: "Louise", vicePresident: "Steve", secretary: "Fred", treasurer: "Ivy" },
        "Steve": { president: "Tracy", vicePresident: "Kerry", secretary: "Oscar", treasurer: "Xavier" },
        "Tracy": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Ivy" },
        "Ullyses": { president: "Louise", vicePresident: "Hermann", secretary: "Ivy", treasurer: "Bob" },
        "Valorie": { president: "Wesley", vicePresident: "Bob", secretary: "Alex", treasurer: "Ivy" },
        "Wesley": { president: "Bob", vicePresident: "Yvonne", secretary: "Valorie", treasurer: "Ivy" },
        "Xavier": { president: "Steve", vicePresident: "Hermann", secretary: "Fred", treasurer: "Ivy" },
        "Yvonne": { president: "Bob", vicePresident: "Zane", secretary: "Fred", treasurer: "Hermann" },
        "Zane": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Mary" }
    };

    // Tally the votes in voteCount.
    var voteCount = {
        president: {},
        vicePresident: {},
        secretary: {},
        treasurer: {}
    };

    for (var student in votes) {
        if (votes.hasOwnProperty(student)) {
            var office = votes[student];
            for (var prop in office) {
                if (!voteCount["president"].hasOwnProperty(office["president"])) {
                    voteCount["president"][office["president"]] = 0;
                } else {
                    voteCount["president"][office["president"]] += 1;
                }
            }
        }
    }

Comments

1

Fixed your code a little bit, hope I didn't break the logic:

for (var student in votes) {
  if (votes.hasOwnProperty(student)) {
    var office = votes[student];
    for (var prop in office) {
      // init property with 0
      if (!voteCount[prop][office[prop]]) {
        voteCount[prop][office[prop]] = 0;
      }

      // add vote
      voteCount[prop][office[prop]]++;
    }
  }
}

Fiddle is here.

UPD: made it work for all office.

3 Comments

hey, ok, but it seems to count things 4 times, rather than just once??
Checked for Bob - 3 times for president, 4 times for treasurer. Looks correct to me. Did you open the updated link?
Hmm, silly me. Did do something wrong. Thank you for your help!

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.