我想用下面的函數的輸出創建一個對象。寫入循環中的對象
var threads = {
\t "thread1": {
\t \t "upvotes": {
\t \t \t "8fsygfs": true,
\t \t \t "atw9g87": true,
\t \t \t "atw923swg87": true,
\t \t \t "34j2njk": true
\t \t },
\t \t "downvotes": {
\t \t \t "jne280n": true,
\t \t \t "892ned9": true,
\t \t \t "28hd0ye": true,
\t \t \t "cjsnd09": true,
\t \t \t "02jd0d2": true,
\t \t }
\t },
\t "thread2": {
\t \t "upvotes": {
\t \t \t "02jd0d2": true,
\t \t \t "8fsygfs": true,
\t \t \t "7dr4229": true,
\t \t \t "232c3f25": true,
\t \t \t "34j2njk": true,
\t \t \t "atw9g87": true,
\t \t \t "atw923swg87": true,
\t \t },
\t \t "downvotes": {
\t \t \t "jne280n": true,
\t \t \t "9ah8229": true,
\t \t \t "89h208d": true,
\t \t \t "28hd0ye": true,
\t \t \t "cjsnd09": true
\t \t }
\t },
\t "thread3": {
\t \t "upvotes": {
\t \t \t "02jd0d2": true,
\t \t \t "9ah8229": true,
\t \t \t "838w32l": true,
\t \t \t "78awg2l": true,
\t \t \t "34j2njk": true
\t \t },
\t \t "downvotes": {
\t \t \t "jne280n": true,
\t \t \t "atw9g87": true,
\t \t \t "892ned9": true,
\t \t \t "28hd0ye": true
\t \t }
\t }
}
var members = [
\t "8fsygfs",
\t "atw9g87",
\t "atw923swg87",
\t "34j2njk",
\t "jne280n",
\t "892ned9",
\t "28hd0ye",
\t "cjsnd09",
\t "02jd0d2",
\t "9ah8229",
\t "9ah8229",
\t "7dr4229",
\t "232c3f25",
\t "838w32l",
\t "78awg2l"
]
function getAlignmentSets() {
var checked = []
members.forEach(k => {
\t members.forEach(l => {
\t \t var matches = 0
\t \t if (k != l && (!checked.includes(l))) {
\t \t \t Object.keys(threads).forEach(m => {
\t \t \t \t var thread = Object.keys(threads[m].upvotes).concat(Object.keys(threads[m].downvotes))
\t \t \t \t if (thread.includes(k) && thread.includes(l)) {
\t \t \t \t \t matches++
\t \t \t \t }
\t \t \t })
\t \t \t console.log(k + ": { " + l + ": " + matches + " }")
\t \t }
\t })
\t checked.push(k)
})
}
getAlignmentSets()
此函數計算的時候兩個用戶投票在同一個線程總數 - 不管他們是否投票相同或不同。我需要它輸出的對象,看起來像這樣:
"member1": {
"member2": 2, // value is the number of times the this member has
"member3": 1, // voted on the same thread as its parent
...
},
"member2": {
"member3": 2,
...
},
...
我與財產支架存取的書面概念寫作的兒童在奮力對象的循環中,更是如此。在此先感謝您的幫助。
我把它改成你的建議。 –