使用循环填充对象

时间:2017-03-13 17:09:58

标签: javascript javascript-objects

考虑以下模型:

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中的其他数字一致。这似乎是一个基本问题,但我现在正在努力解决这个问题。

1 个答案:

答案 0 :(得分:2)

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

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

配对解决方案。

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;