次の関数の出力でオブジェクトを作成したいと思います。かかわらず、彼らは異なっ同じかを投票するかどうかの -ループ内のオブジェクトへの書き込み
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()
この関数は、2人のユーザーが同じスレッドに投票した合計回数をカウントします。子どもを書くとき、私は特に、ループ内のオブジェクトへの書き込みで、プロパティブラケットアクセサの概念に苦しんだ
"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,
...
},
...
:私は、出力に次のようになりますオブジェクトを、それを必要とします。あなたの助けを前にありがとう。
私はあなたの提案にそれを変更しました。 –