gpt4 book ai didi

javascript - 用循环填充对象

转载 作者:行者123 更新时间:2023-11-30 15:27:14 24 4
gpt4 key购买 nike

考虑以下模型:

var threads = {
"thread1": {
"upvotes": {
"1": true,
"3": true,
"4": true,
"10": true
},
"downvotes": {
"2": true,
"5": true,
"8": true,
"9": true
}
},
"thread2": {
"upvotes": {
"1": true,
"3": true,
"7": true,
"10": true
},
"downvotes": {
"2": true,
"6": true,
"8": true,
"9": true
}
},
"thread3": {
"upvotes": {
"1": true,
"4": true,
"7": true,
"10": true
},
"downvotes": {
"2": true,
"5": true,
"8": true
}
}
}

我想迭代这个模型,获取每个实例的计数,其中 upvotes 中的每个数字与 upvotes 中的每个其他数字一致。我正在执行以下操作以获得对此的粗略估计:

var us = 0;
var youObj = {};
var matchesObj = {};
members = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];

var getAlignments = function(me, you) {
for (var thread in threads) {
if ((Object.keys(threads[thread].upvotes).includes(me)) && (Object.keys(threads[thread].upvotes).includes(you))) {
us++
}
}
if (us > 0) {
youObj[you] = us
matchesObj[me] = youObj
us = 0;
}
}

for (var i = 0; i < members.length; i++) {
var me = members[i]
for (var j = 0; j < members.length; j++) {
var you = members[j]
getAlignments(me, you)
}
}

console.log(matchesObj)

这会将以下内容记录到控制台:

{ 
'1': { '1': 3, '3': 2, '4': 2, '7': 2, '10': 3 },
'3': { '1': 3, '3': 2, '4': 2, '7': 2, '10': 3 },
'4': { '1': 3, '3': 2, '4': 2, '7': 2, '10': 3 },
'7': { '1': 3, '3': 2, '4': 2, '7': 2, '10': 3 },
'10': { '1': 3, '3': 2, '4': 2, '7': 2, '10': 3 }
}

如您所见,子对象都是相同的。原因很明显。循环中分配给其父对象的最后一个对象将覆盖前一个对象。 { '1': 3, '3': 2, '4': 2, '7': 2, '10': 3 } 对象中的每个属性键代表一个与'10' 在每个线程的 upvotes 对象中,每个属性值代表这些巧合的计数。

我需要的是这种类型的列表,每个数字都与 upvotes 中的其他数字一致。这似乎是一个基本问题,但我现在正在努力解决这个问题。

最佳答案

为什么不对键使用双重嵌套循环并获取计数。

var threads = { thread1: { upvotes: { 1: true, 3: true, 4: true, 10: true }, downvotes: { 2: true, 5: true, 8: true, 9: true } }, thread2: { upvotes: { 1: true, 3: true, 7: true, 10: true }, downvotes: { 2: true, 6: true, 8: true, 9: true } }, thread3: { upvotes: { 1: true, 4: true, 7: true, 10: true }, downvotes: { 2: true, 5: true, 8: true } } },
result = {};

Object.keys(threads).forEach(function (k) {
Object.keys(threads[k].upvotes).forEach(function (l) {
result[l] = (result[l] || 0) + 1;
});
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

成对的解决方案。

var threads = { thread1: { upvotes: { 1: true, 3: true, 4: true, 10: true }, downvotes: { 2: true, 5: true, 8: true, 9: true } }, thread2: { upvotes: { 1: true, 3: true, 7: true, 10: true }, downvotes: { 2: true, 6: true, 8: true, 9: true } }, thread3: { upvotes: { 1: true, 4: true, 7: true, 10: true }, downvotes: { 2: true, 5: true, 8: true } } },
result = {},
result2 = {};

Object.keys(threads).forEach(function (k) {
var keys = Object.keys(threads[k].upvotes);
keys.forEach(function (l) {
result[l] = (result[l] || 0) + 1;
result2[l] = result2[l] || {};
keys.forEach(function (m) {
if (l !== m) {
result2[l][m] = (result2[l][m] || 0) + 1;
}
});
});
});


console.log(result);
console.log(result2);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 用循环填充对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42769465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com