获取数组之间的合并差异 - 在这种情况下需要同步数组

Get consolidated difference between arrays - from the need to sync arrays in this situation

我不确定我是否在标题中最好地描述了问题,但我会在这里详细说明。

我的总体目标是保持列表同步,我目前正在尝试获取特定的输出,以便稍后可以更正列表对称性。

我已经想通了这一点:

代码:

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

这会输出太多数据,我想压缩它

期望的输出是压缩所有数据,以便“需要”的 none 重复,因此,例如,如果用户 #2 可以从用户 #1 获得 1 和 10,则无需输出用户#2 需要来自用户#3 的 1... 你跟着我吗?我认为这可以变得简单,但我只是不知道可以轻松完成此操作的任何操作。

这是我试图实现的输出模型(理想情况下):

[
{"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 获取。

有点循环逻辑的噩梦,如果能帮助解决这个问题,我将不胜感激。为了实现这一点,我只得到了更多令人困惑的输出。

你能使用类似下面的代码吗?

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]}
];

const result = []
for (let comparator = 0; comparator < list2.length; comparator++) {
  const withAdd = [...list2[comparator].log]
  result[comparator] = {user:list2[comparator].user,symmetriseLogs:[]}
  for (let index = 0; index < list2.length; index++) {
    if (comparator !== index) {
        const currentDiff = list2[index].log.filter(x => !withAdd.includes(x));
        if (currentDiff.length) {
            console.log("User: " + list2[comparator].user + " needs " + currentDiff + " from user: " + list2[index].user);
            result[comparator].symmetriseLogs.push({user:list2[index].user, missingLogs:currentDiff})
        }
        withAdd.push(...currentDiff)
    }
  }
}
console.log(result)

根据您的输入,我得到以下输出:

  • 用户:1 需要 44 来自用户:2
  • 用户:2 需要来自用户:1 的 1,10
  • 用户:3 需要用户:1 的 5、9、10
  • 用户:3 需要 44 来自用户:2
  • 用户:4 需要来自用户:1 的 9,10
  • 用户:4 需要 44 来自用户:2

并且:

[
    {
        "user": 1,
        "symmetriseLogs": [
            {
                "user": 2,
                "missingLogs": [
                    44
                ]
            }
        ]
    },
    {
        "user": 2,
        "symmetriseLogs": [
            {
                "user": 1,
                "missingLogs": [
                    1,
                    10
                ]
            }
        ]
    },
    {
        "user": 3,
        "symmetriseLogs": [
            {
                "user": 1,
                "missingLogs": [
                    5,
                    9,
                    10
                ]
            },
            {
                "user": 2,
                "missingLogs": [
                    44
                ]
            }
        ]
    },
    {
        "user": 4,
        "symmetriseLogs": [
            {
                "user": 1,
                "missingLogs": [
                    9,
                    10
                ]
            },
            {
                "user": 2,
                "missingLogs": [
                    44
                ]
            }
        ]
    }
]

一种方法是,在开始迭代之前,您可以制作一个镜像结构,将每个用户映射到它目前拥有的日志。在循环内,查找用户的现有日志以查看需要添加哪些数字。

它不像 .map 预期的那样 纯粹 ,但它完成了工作,我想不出更好看的方法。

const 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]}
];

const haveLogsByUserId = new Map(list2.map(({ user, log }) => [user, new Set(log)]));

const result = list2.map((source, i) => ({
  user: source.user,
  symmetriseLogs: list2
    .filter((_, j) => i !== j)
    .map(dest => {
      const thisUserLogs = haveLogsByUserId.get(dest.user);
      const missingLogs = source.log.filter(num => !thisUserLogs.has(num));
      for (const num of missingLogs) thisUserLogs.add(num);
      return { user: dest.user, missingLogs };
    })
    .filter(missingObj => missingObj.missingLogs.length)
}));
console.log(result);