2017-03-24 4 views
0

Ich möchte ein Objekt mit der Ausgabe der folgenden Funktion erstellen.In ein Objekt innerhalb einer Schleife schreiben

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

Diese Funktion zählt die Gesamtzahl der für zwei Benutzer auf dem gleichen Thread abgestimmt - unabhängig davon, ob sie gleich oder unterschiedlich gewählt. Ich brauche es, um ein Objekt zu Ausgabe, die wie folgt aussieht:

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

ich mit dem Begriff des Eigentum Klammer Accessoren schriftlich auf ein Objekt in einer Schleife bin zu kämpfen, vor allem, wenn Kinder zu schreiben. Vielen Dank im Voraus für Ihre Hilfe.

+0

ich habe mich verändert, um sie Ihren Vorschlag. –

Antwort

1

Angenommen, Ihre Logik war korrekt, Sie schreiben es einfach nicht an etwas für die Rückkehr. Ich nehme an, das ist was coappearances war für. Sie fehlt dies:

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

Beispiel unten:

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

Das macht, was ich brauchte, aber es zeigt einen Fehler mit meiner Logik. Ich versuchte zu vermeiden, Paare zweimal zu überprüfen, aber es scheint ein unbeabsichtigtes Problem verursacht zu haben. In jedem Fall ist es leicht zu beheben. Vielen Dank. Kannst du bitte 'coverance [k] [l]' ohne Bracket-Notation ausdrücken? Das ist seltsam für mich, weil 'coverance [k]' einfach "memberId" 'ist, also scheint' cappeerstandes [k] [l] 'entweder" memberId "[l]' oder "memberId" 'zu sein (weil es einfach den Namen des zweiten Accessors nehmen würde). –

1

Ich weiß, Sie haben bereits eine Antwort akzeptiert, wollte aber einen alternativen Ansatz schaffen, die wesentlich mehr ist performant unter Verwendung von Set mit den eindeutigen Mitgliedern in jedem Thread:

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

Sie können den Unterschied auf JSPerf Check-out: https://jsperf.com/getalignmentset-versions/1

+0

Ich behalte die ursprüngliche Antwort, weil die Frage nicht nach einer Bewertung der Funktion selbst fragt, aber das ist sehr lehrreich. Vielen Dank. –

+0

Definitiv, dass die erste Antwort eine bessere Antwort auf Ihre Frage ist und als solche gekennzeichnet werden sollte. Keine Sorge, ich hoffe es ist hilfreich –