简体   繁体   中英

looping through objects in objects and then adding properties to another object in javascript

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,

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.

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;
                }
            }
        }
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM