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.
ich habe mich verändert, um sie Ihren Vorschlag. –