2017-03-24 16 views
0

次の関数の出力でオブジェクトを作成したいと思います。かかわらず、彼らは異なっ同じかを投票するかどうかの -ループ内のオブジェクトへの書き込み

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, 
    ... 
}, 
... 

:私は、出力に次のようになりますオブジェクトを、それを必要とします。あなたの助けを前にありがとう。

+0

私はあなたの提案にそれを変更しました。 –

答えて

1

あなたのロジックが正しいと仮定すると、あなたは戻ってくるためにそれを書いているだけではありません。私はこれがcoappearancesのためだと仮定します。私はあなたが答えをすでに受け入れてきました知っているが、はるかにある別のアプローチを提供したかった

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 coappearances = {} 
 
    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 + " }") 
 
       if(!coappearances[k]) 
 
        coappearances[k] = {}; 
 
       coappearances[k][l] = matches; 
 
\t \t  } 
 
\t  }) 
 
\t  checked.push(k) 
 
    }) 
 
    return coappearances; 
 
} 
 

 
var result = getAlignmentSets(); 
 
console.log(result);

+0

これは私が必要としたことをしますが、それは私の論理に欠陥を明らかにします。ペアを2回チェックするのを避けようとしていましたが、意図しない問題を引き起こしたようです。いずれにしても、簡単に修正できます。ありがとう。あなたはカッコ表記を使わずに 'coappearances [k] [l]'を表現できますか? 'coappearances [k]'は単純に '' memberId''なので、これは私にとっては奇妙なことです。 '' coappearances [k] [l] 'は' 'memberId" [l] 'または' 'memberId'' (これは単に第2アクセサの名前をとるためです)。 –

1

:以下

if(!coappearances[k]) 
    coappearances[k] = {}; 
coappearances[k][l] = matches; 

例:あなたはこれを逃しています各スレッド内のユニークなメンバーを含むSetを使用する演奏者:

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() { 
 
    // turn each thread into a Set of participating members 
 
    const threadSets = Object.keys(threads).reduce((prev, thread) => { 
 
     prev[thread] = new Set(
 
     [].concat(Object.keys(threads[thread].upvotes), Object.keys(threads[thread].downvotes)) 
 
    ); 
 
     return prev; 
 
    }, {}); 
 

 
    // on each iteration, check if current user appears in each thread 
 
    // reduce each additional member in the thread to a memberId => voteCount pair 
 
    return members.reduce((memberData, curMember) => { 
 
     memberData[curMember] = {}; 
 
     
 
     Object.keys(threadSets).forEach((set) => { 
 
      const curThread = threadSets[set]; 
 
      if (curThread.has(curMember)) { 
 
       Array.from(curThread.values()).reduce((p, userKey) => { 
 
        if (userKey === curMember) { 
 
         return p; 
 
        } else if (userKey in p) { 
 
         p[userKey] = p[userKey] + 1; 
 
        } else { 
 
         p[userKey] = 1; 
 
        } 
 
        return p; 
 
       }, memberData[curMember]); 
 
      } 
 
     }); 
 
     return memberData; 
 
    }, {}); 
 
} 
 

 
console.log(getAlignmentSets());

あなたはJSPerf上の違いをチェックアウトすることができます:https://jsperf.com/getalignmentset-versions/1

+0

質問は機能自体の評価を求めるものではないので、私は元の答えを保持するつもりですが、これは非常に有益です。ありがとうございました。 –

+0

最初の回答があなたの質問に対するより良い回答であり、そのようにマークされるべきであることは間違いありません。心配しないで、それが役に立つと願っています –

関連する問題