looping through objects in objects and then adding properties to another object in javascript -
want able add properties object in object separate object, have got working point 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 votes in votecount. var votecount = { president: {}, vicepresident: {}, secretary: {}, treasurer: {} }; (var student in votes) { if (votes.hasownproperty(student)) { var office = votes[student]; (var prop in office) { if (!office.hasownproperty("president")) { votecount["president"][office["president"]] = 0; } else { votecount["president"][office["president"]] += 1; } } } }
the above in loop return nan
!!!
getting stuff across votecount
not numbers of votes??
votecount
becomes
votecount { president: { bob: nan, mary: nan, cindy: nan, louise: nan, fred: nan,
i think error in addition of properties result object.
this different approach, builds results object dynamically out of votes role.
var results = {}; var n,r,v; (n in votes) { (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, better put vars @ beginning of code, placing them in fors not limit scope, declared @ parse time , global.