2

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

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

我已经弄清楚了这一点:

代码:

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 获得。

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

4

2 回答 2

2

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

它并不像预期的那样纯粹.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);

于 2021-12-11T23:38:26.230 回答
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,10 来自用户:1
  • 用户:3 需要 5,9,10 来自用户:1
  • 用户:3 需要 44 来自用户:2
  • 用户:4 需要 9,10 来自用户:1
  • 用户: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
                ]
            }
        ]
    }
]
于 2021-12-11T23:37:45.057 回答