2015-04-23 69 views
0

希望能夠將對象中的某個對象的屬性添加到單獨的對象,讓它工作到某個特定點,但是會得到NaN返回?循環瀏覽對象中的對象,然後向javascript中的另一個對象添加屬性

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

以上代表循環返回NaN !!!
我得到的東西跨voteCount但不是票數?

voteCount成爲

voteCount 
{ president: 
    { Bob: NaN, 
    Mary: NaN, 
    Cindy: NaN, 
    Louise: NaN, 
    Fred: NaN, 
+0

你確定你確實是指'office.hasOwnProperty(「president」)'?在我看來,你可能想用'voteCount [prop] .hasOwnProperty(office [prop])'代替。 – Passerby

+0

嗯,試過,但它給每個3。而不是每個人/財產的實際票數。 – SchuKnight

回答

1

我認爲你的錯誤是在結果對象的屬性中增加的。

這是一種不同的方法,它根據角色動態構建結果對象。

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

而且,最好是把所有的瓦爾在代碼的開始,將它們放置在維權不限制其範圍,他們在分析時宣稱,將是全球性的。

1

修正你的代碼一點點,希望我沒有打破邏輯:

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

小提琴是here

UPD:使其適用於所有辦公室。

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

這樣::

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

最終的結果是:

+0

嘿,好吧,但它似乎計數的東西4次,而不是一次? – SchuKnight

+0

檢查鮑勃 - 總裁3次,司庫4次。看起來對我來說是正確的。你打開更新的鏈接了嗎? –

+1

嗯,傻我。做錯了什麼。感謝您的幫助! – SchuKnight

1

您可以通過改變來檢查,如果總統辦公室hasOwnProperty行解決這個

 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; 
       } 
      } 
     } 
    } 
相關問題