我不确定我是否在标题中最好地描述了这个问题,但我会在这里详细说明。
我的总体目标是保持列表同步,我目前正在尝试获得特定的输出,以便以后可以更正列表对称性。
我已经弄清楚了这一点:
代码:
let list2 = [
{user: 001, log: [1,2,3,4,5,6,7,8,9,10]},
{user: 002, log: [2,3,4,5,6,7,8,9, 44]},
{user: 003, log: [1,2,3,4,6,7,8]},
{user: 004, log: [1,2,3,4,5,6,7,8]}
];
for (let comparator = 0; comparator < list2.length; comparator++) {
for (let index = 0; index < list2.length; index++) {
if (comparator !== index) {
let currentDiff = list2[comparator].log.filter(x => !list2[index].log.includes(x));
console.log("User: " + list2[index].user + " needs " + currentDiff + " from user: " + list2[comparator].user);
}
}
}
输出:
User: 2 needs 1,10 from user: 1
User: 3 needs 5,9,10 from user: 1
User: 4 needs 9,10 from user: 1
User: 1 needs 44 from user: 2
User: 3 needs 5,9,44 from user: 2
User: 4 needs 9,44 from user: 2
User: 1 needs from user: 3
User: 2 needs 1 from user: 3
User: 4 needs from user: 3
User: 1 needs from user: 4
User: 2 needs 1 from user: 4
User: 3 needs 5 from user: 4
这输出了太多的数据&我想压缩它
所需的输出是所有数据都被压缩,这样“需要”就不会重复,所以例如,如果用户#2 可以从用户 #1 获得 1 和 10,那么就不需要输出用户 #2 需要用户 # 的 1 3……你跟着我吗?我认为这可以变得简单,但我只是不知道任何可以轻松完成此操作的操作。
这是我想要实现的输出模型(理想情况下):
[
{"user": 1,
"symmetriseLogs": [
{user: 2, missingLogs: [1, 10]},
{user: 3, missingLogs: [5, 9, 10]},
{user: 4, missingLogs: [9, 10]},
]},
{"user": 2,
"symmetriseLogs": [
{user: 1, missingLogs: [44]},
{user: 3, missingLogs: [44]},
{user: 4, missingLogs: [44]},
]},
]
输出应该是对称化所有日志所需的,因此在示例输出中,用户 #1 和 #2 缺少的所有内容都可以相互获取,因此用户 #3 和 #4 不会获得输出。用户#2 也只需要输出 44,因为这是唯一的日志项目 44 有其他人丢失并且无法从用户 #1 获得。
有点循环逻辑噩梦,我将不胜感激任何帮助解决这个问题。为了实现这一目标,我只得到了更多令人困惑的输出。