获得阵列之间的综合差异 - 在这种情况下需要同步阵列

Posted

技术标签:

【中文标题】获得阵列之间的综合差异 - 在这种情况下需要同步阵列【英文标题】:Get consolidated difference between arrays - from the need to sync arrays in this situation 【发布时间】:2022-01-16 01:08:36 【问题描述】:

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

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

我已经想通了:

代码:

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

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

【问题讨论】:

【参考方案1】:

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

它并不像.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);

【讨论】:

这正是我想要的。另外,您使我的代码变得更好。这扩展了我的知识和经验,谢谢【参考方案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]
];

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 需要来自用户:2 的 44 用户:2 需要来自用户:1 的 1,10 用户:3 需要来自用户:1 的 5、9、10 用户:3 需要来自用户:2 的 44 用户:4 需要来自用户:1 的 9,10 用户:4 需要来自用户:2 的 44

还有:

[
    
        "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
                ]
            
        ]
    
]

【讨论】:

以上是关于获得阵列之间的综合差异 - 在这种情况下需要同步阵列的主要内容,如果未能解决你的问题,请参考以下文章

线阵相机的原理和线阵相机为啥做黑白平衡

相控阵雷达天线不同阵列的特性matlab仿真分析——有限扫描阵,稀疏阵,多波束阵,共形阵

非均匀阵线阵前后向平滑仿真

Impala 获得 2 个日期之间的差异,不包括周末

python 随机比特阵发生器,用于测试比特阵列功能

两个矩形之间的差异(XOR),作为矩形?