写入循环中的对象

时间:2017-03-24 16:49:21

标签: javascript

我想用以下函数的输出创建一个对象。

var threads = {
	"thread1": {
		"upvotes": {
			"8fsygfs": true,
			"atw9g87": true,
			"atw923swg87": true,
			"34j2njk": true
		},
		"downvotes": {
			"jne280n": true,
			"892ned9": true,
			"28hd0ye": true,
			"cjsnd09": true,
			"02jd0d2": true,
		}
	},
	"thread2": {
		"upvotes": {
			"02jd0d2": true,
			"8fsygfs": true,
			"7dr4229": true,
			"232c3f25": true,
			"34j2njk": true,
			"atw9g87": true,
			"atw923swg87": true,
		},
		"downvotes": {
			"jne280n": true,
			"9ah8229": true,
			"89h208d": true,
			"28hd0ye": true,
			"cjsnd09": true
		}
	},
	"thread3": {
		"upvotes": {
			"02jd0d2": true,
			"9ah8229": true,
			"838w32l": true,
			"78awg2l": true,
			"34j2njk": true
		},
		"downvotes": {
			"jne280n": true,
			"atw9g87": true,
			"892ned9": true,
			"28hd0ye": true
		}
	}
}

var members = [
	"8fsygfs",
	"atw9g87",
	"atw923swg87",
	"34j2njk",
	"jne280n",
	"892ned9",
	"28hd0ye",
	"cjsnd09",
	"02jd0d2",
	"9ah8229",
	"9ah8229",
	"7dr4229",
	"232c3f25",
	"838w32l",
	"78awg2l"
]

function getAlignmentSets() {
    var checked = []
    members.forEach(k => {
	    members.forEach(l => {
		    var matches = 0
		    if (k != l && (!checked.includes(l))) {
			    Object.keys(threads).forEach(m => {
				    var thread = Object.keys(threads[m].upvotes).concat(Object.keys(threads[m].downvotes))
				    if (thread.includes(k) && thread.includes(l)) {
					    matches++
				    }
			    })
			    console.log(k + ": { " + l + ": " +  matches + " }" )
		    }
	    })
	    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,
    ...
},
...

在写入循环中的对象时,我正在努力使用属性括号访问器的概念,尤其是在编写子代时。在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

假设你的逻辑是正确的,你只是不把它写回任何东西以便返回。我认为这是coappearances的用途。你错过了这个:

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

以下示例:

var threads = {
	"thread1": {
		"upvotes": {
			"8fsygfs": true,
			"atw9g87": true,
			"atw923swg87": true,
			"34j2njk": true
		},
		"downvotes": {
			"jne280n": true,
			"892ned9": true,
			"28hd0ye": true,
			"cjsnd09": true,
			"02jd0d2": true,
		}
	},
	"thread2": {
		"upvotes": {
			"02jd0d2": true,
			"8fsygfs": true,
			"7dr4229": true,
			"232c3f25": true,
			"34j2njk": true,
			"atw9g87": true,
			"atw923swg87": true,
		},
		"downvotes": {
			"jne280n": true,
			"9ah8229": true,
			"89h208d": true,
			"28hd0ye": true,
			"cjsnd09": true
		}
	},
	"thread3": {
		"upvotes": {
			"02jd0d2": true,
			"9ah8229": true,
			"838w32l": true,
			"78awg2l": true,
			"34j2njk": true
		},
		"downvotes": {
			"jne280n": true,
			"atw9g87": true,
			"892ned9": true,
			"28hd0ye": true
		}
	}
}

var members = [
	"8fsygfs",
	"atw9g87",
	"atw923swg87",
	"34j2njk",
	"jne280n",
	"892ned9",
	"28hd0ye",
	"cjsnd09",
	"02jd0d2",
	"9ah8229",
	"9ah8229",
	"7dr4229",
	"232c3f25",
	"838w32l",
	"78awg2l"
]

function getAlignmentSets() {
    var coappearances = {}
    var checked = []
    members.forEach(k => {
	    members.forEach(l => {
		    var matches = 0
		    if (k != l && (!checked.includes(l))) {
			    Object.keys(threads).forEach(m => {
				    var thread = Object.keys(threads[m].upvotes).concat(Object.keys(threads[m].downvotes))
				    if (thread.includes(k) && thread.includes(l)) {
					    matches++
				    }
			    })
			    //console.log(k + ": { " + l + ": " +  matches + " }" )
                if(!coappearances[k])
                   coappearances[k] = {};
                coappearances[k][l] = matches;
		    }
	    })
	    checked.push(k)
    })
    return coappearances;
}

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

答案 1 :(得分:1)

我知道您已经接受了答案,但希望提供一种替代方法,使用包含每个帖子中唯一成员的Set来提高性能:



var threads = {
	"thread1": {
		"upvotes": {
			"8fsygfs": true,
			"atw9g87": true,
			"atw923swg87": true,
			"34j2njk": true
		},
		"downvotes": {
			"jne280n": true,
			"892ned9": true,
			"28hd0ye": true,
			"cjsnd09": true,
			"02jd0d2": true,
		}
	},
	"thread2": {
		"upvotes": {
			"02jd0d2": true,
			"8fsygfs": true,
			"7dr4229": true,
			"232c3f25": true,
			"34j2njk": true,
			"atw9g87": true,
			"atw923swg87": true,
		},
		"downvotes": {
			"jne280n": true,
			"9ah8229": true,
			"89h208d": true,
			"28hd0ye": true,
			"cjsnd09": true
		}
	},
	"thread3": {
		"upvotes": {
			"02jd0d2": true,
			"9ah8229": true,
			"838w32l": true,
			"78awg2l": true,
			"34j2njk": true
		},
		"downvotes": {
			"jne280n": true,
			"atw9g87": true,
			"892ned9": true,
			"28hd0ye": true
		}
	}
}

var members = [
	"8fsygfs",
	"atw9g87",
	"atw923swg87",
	"34j2njk",
	"jne280n",
	"892ned9",
	"28hd0ye",
	"cjsnd09",
	"02jd0d2",
	"9ah8229",
	"9ah8229",
	"7dr4229",
	"232c3f25",
	"838w32l",
	"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