比较两个数组并在JS中添加丢失的对象

3
我有两个大数组,我想比较这两个数组并将arrayOne中缺失的数据添加到arrayTwo中。
这是我的一些数据。
const arrayOne = [
  {
    id: "This Year",
    data: [
      { x: "01-02", y: "81" },
      { x: "01-03", y: "361" },
      { x: "01-04", y: "64" },
      { x: "01-05", y: "169" },
      { x: "01-06", y: "9" },
      { x: "01-07", y: "100" },
      { x: "01-08", y: "144" },
      { x: "01-09", y: "81" },
      { x: "01-10", y: "256" },
      { x: "01-11", y: "81" },
      { x: "01-12", y: "144" },
      { x: "01-13", y: "144" },
      { x: "01-14", y: "225" },
      { x: "01-15", y: "289" },
      { x: "01-16", y: "81" },
      { x: "01-17", y: "64" },
      { x: "01-18", y: "64" },
      { x: "01-19", y: "121" },
      { x: "01-20", y: "25" },
      { x: "01-21", y: "49" },
      { x: "01-22", y: "16" },
      { x: "01-23", y: "49" },
      { x: "01-24", y: "196" },
      { x: "01-25", y: "16" },
      { x: "01-26", y: "25" },
      { x: "01-27", y: null },
      { x: "01-28", y: "144" },
      { x: "01-29", y: "100" },
      { x: "01-30", y: "64" },
      { x: "01-31", y: "144" },
      { x: "02-01", y: "100" },
      { x: "02-02", y: "100" },
      { x: "02-03", y: "49" },
    ],
  },
];

const arrayTwo = [
  {
    id: "This Year",
    data: [
      { x: "01-02", y: "64" },
      { x: "01-03", y: "25" },
      { x: "01-04", y: "25" },
      { x: "01-05", y: "169" },
      { x: "01-15", y: "64" },
      { x: "01-16", y: "121" },
      { x: "01-17", y: "49" },
      { x: "01-18", y: "81" },
      { x: "01-19", y: "49" },
    ],
  },
];

我曾试图将其映射并与x进行比较,但我无法获得理想的输出。

arrayOne[0].data.map((date, index) => {
    arrayTwo[0].data.map((newDate, newIndex) => {
  if (date.x !== newDate.x) {
      arrayTwo[0].data.push({x:date.x, y: null })
    }
    });
  });

我想要检查 arrayTwo[data] 中是否有丢失的数据,如果有则从 arrayOne[data] 中添加该数据(即获取具有相同 x 值的对象,但将 y 值设置为 null)。

期望输出:

[
  {
    "id":"This Year",
    "data":[
      {"x":"01-02", "y":"64"},
      {"x":"01-03", "y":"25"},
      {"x":"01-04", "y":"25"},
      {"x":"01-05", "y":"169"},
      {"x":"01-06", "y":null},
      {"x":"01-07", "y":null},
      {"x":"01-08", "y":null},
      {"x":"01-09", "y":null},
      {"x":"01-10", "y":null},
      {"x":"01-11", "y":null},
      {"x":"01-12", "y":null},
      {"x":"01-13", "y":null},
      {"x":"01-14", "y":null},
      {"x":"01-15", "y":"64"},
      {"x":"01-16", "y":"121"},
      {"x":"01-17", "y":"49"},
      {"x":"01-18", "y":"81"},
      {"x":"01-19", "y":"49"},
      {"x":"01-20", "y":null},
      {"x":"01-21", "y":null},
      {"x":"01-22", "y":null},
      {"x":"01-23", "y":null},
      {"x":"01-24", "y":null},
      {"x":"01-25", "y":null},
      {"x":"01-26", "y":null},
      {"x":"01-27", "y":null},
      {"x":"01-28", "y":null},
      {"x":"01-29", "y":null},
      {"x":"01-30", "y":null},
      {"x":"01-31", "y":null},
      {"x":"02-01", "y":null},
      {"x":"02-02", "y":null},
      {"x":"02-03", "y":null}
    ]
  }
]

问题是什么? - Andreas
@Andreas 我无法实现它。 - Rizwan Ahmed Shivalli
比较的关键是什么?还是您想再次检查整个对象与整个对象? - EugenSunic
你的输出中是想要来自arrayOne的 {"x": "01-19", "y": "121"},还是想要存在于arrayTwo中的以下内容 {"x": "01-19", "y": "49"}?从你的问题和@EugenSunic的回答来看,我认为后者是相反的(将行为反转将是一个简单的修复)。 - Alex L
@AlexL 现有的数据不应被干扰,如果arrayTwo中不存在该数据,则应从arrayOne中获取x值,y值应为null。例如,从arrayOne获取{ x: "02-03", y: "49" },则在arrayTwo中输出应为{ x: "02-03", y: null } - Rizwan Ahmed Shivalli
1
好的,所以以下两个答案都没有提供那种行为。我现在已经将所需的输出添加到您的问题中,请确认是否正确。如果是这样,我的更新答案(第二个示例)现在可以提供所需的输出。 - Alex L
2个回答

2
你可以这样做:
  • 使用reduce从arrayOne [0] .data创建一个对象(objOne
  • 如果在arrayTwo [0] .data中存在键,则使用reduce覆盖objOne的任何属性,并使用objOne作为初始值。
  • 使用Object.values(objOne)将此objOne转换为数组,然后将其设置为arrayTwo [0] .data的属性
两个reduce函数的时间复杂度应为O(n + m)(其中n和m是两个数组的长度)。 (应该比在其中一个数组的每个元素上使用find更快)
代码的关键部分:
const objOne = arrayOne[0].data.reduce((aggObj, item) => {
  aggObj[item.x] = item;  
  return aggObj;
}, {});

const mergedObjOutput = arrayTwo[0].data.reduce((aggObj, item) => {
  aggObj[item.x] = item;  
  return aggObj;
}, objOne)

const mergedFinalOutput = [...arrayTwo];
mergedFinalOutput[0].data = Object.values(mergedObjOutput);

console.log(mergedFinalOutput);

完整演示:

const arrayOne = [
  {
    id: "This Year",
    data: [
      { x: "01-02", y: "81" },
      { x: "01-03", y: "361" },
      { x: "01-04", y: "64" },
      { x: "01-05", y: "169" },
      { x: "01-06", y: "9" },
      { x: "01-07", y: "100" },
      { x: "01-08", y: "144" },
      { x: "01-09", y: "81" },
      { x: "01-10", y: "256" },
      { x: "01-11", y: "81" },
      { x: "01-12", y: "144" },
      { x: "01-13", y: "144" },
      { x: "01-14", y: "225" },
      { x: "01-15", y: "289" },
      { x: "01-16", y: "81" },
      { x: "01-17", y: "64" },
      { x: "01-18", y: "64" },
      { x: "01-19", y: "121" },
      { x: "01-20", y: "25" },
      { x: "01-21", y: "49" },
      { x: "01-22", y: "16" },
      { x: "01-23", y: "49" },
      { x: "01-24", y: "196" },
      { x: "01-25", y: "16" },
      { x: "01-26", y: "25" },
      { x: "01-27", y: null },
      { x: "01-28", y: "144" },
      { x: "01-29", y: "100" },
      { x: "01-30", y: "64" },
      { x: "01-31", y: "144" },
      { x: "02-01", y: "100" },
      { x: "02-02", y: "100" },
      { x: "02-03", y: "49" },
    ],
  },
];

const arrayTwo = [
  {
    id: "This Year",
    data: [
      { x: "01-02", y: "64" },
      { x: "01-03", y: "25" },
      { x: "01-04", y: "25" },
      { x: "01-05", y: "169" },
      { x: "01-15", y: "64" },
      { x: "01-16", y: "121" },
      { x: "01-17", y: "49" },
      { x: "01-18", y: "81" },
      { x: "01-19", y: "49" },
    ],
  },
];

const objOne = arrayOne[0].data.reduce((aggObj, item) => {
  aggObj[item.x] = item;  
  return aggObj;
}, {});

const mergedObjOutput = arrayTwo[0].data.reduce((aggObj, item) => {
  aggObj[item.x] = item;  
  return aggObj;
}, objOne)

const mergedFinalOutput = [...arrayTwo];
mergedFinalOutput[0].data = Object.values(mergedObjOutput);

console.log(mergedFinalOutput);
.as-console-wrapper { max-height: 100% !important; top: 0; }

更新

根据您在问题下评论中的澄清,如果您确实希望输出如下:

[
  {
    "id":"This Year",
    "data":[
      {"x":"01-02", "y":"64"},
      {"x":"01-03", "y":"25"},
      {"x":"01-04", "y":"25"},
      {"x":"01-05", "y":"169"},
      {"x":"01-06", "y":null},
      {"x":"01-07", "y":null},
      {"x":"01-08", "y":null},
      {"x":"01-09", "y":null},
      {"x":"01-10", "y":null},
      {"x":"01-11", "y":null},
      {"x":"01-12", "y":null},
      {"x":"01-13", "y":null},
      {"x":"01-14", "y":null},
      {"x":"01-15", "y":"64"},
      {"x":"01-16", "y":"121"},
      {"x":"01-17", "y":"49"},
      {"x":"01-18", "y":"81"},
      {"x":"01-19", "y":"49"},
      {"x":"01-20", "y":null},
      {"x":"01-21", "y":null},
      {"x":"01-22", "y":null},
      {"x":"01-23", "y":null},
      {"x":"01-24", "y":null},
      {"x":"01-25", "y":null},
      {"x":"01-26", "y":null},
      {"x":"01-27", "y":null},
      {"x":"01-28", "y":null},
      {"x":"01-29", "y":null},
      {"x":"01-30", "y":null},
      {"x":"01-31", "y":null},
      {"x":"02-01", "y":null},
      {"x":"02-02", "y":null},
      {"x":"02-03", "y":null}
    ]
  }
]

然后这是演示:

const arrayOne = [
  {
    id: "This Year",
    data: [
      { x: "01-02", y: "81" },
      { x: "01-03", y: "361" },
      { x: "01-04", y: "64" },
      { x: "01-05", y: "169" },
      { x: "01-06", y: "9" },
      { x: "01-07", y: "100" },
      { x: "01-08", y: "144" },
      { x: "01-09", y: "81" },
      { x: "01-10", y: "256" },
      { x: "01-11", y: "81" },
      { x: "01-12", y: "144" },
      { x: "01-13", y: "144" },
      { x: "01-14", y: "225" },
      { x: "01-15", y: "289" },
      { x: "01-16", y: "81" },
      { x: "01-17", y: "64" },
      { x: "01-18", y: "64" },
      { x: "01-19", y: "121" },
      { x: "01-20", y: "25" },
      { x: "01-21", y: "49" },
      { x: "01-22", y: "16" },
      { x: "01-23", y: "49" },
      { x: "01-24", y: "196" },
      { x: "01-25", y: "16" },
      { x: "01-26", y: "25" },
      { x: "01-27", y: null },
      { x: "01-28", y: "144" },
      { x: "01-29", y: "100" },
      { x: "01-30", y: "64" },
      { x: "01-31", y: "144" },
      { x: "02-01", y: "100" },
      { x: "02-02", y: "100" },
      { x: "02-03", y: "49" },
    ],
  },
];

const arrayTwo = [
  {
    id: "This Year",
    data: [
      { x: "01-02", y: "64" },
      { x: "01-03", y: "25" },
      { x: "01-04", y: "25" },
      { x: "01-05", y: "169" },
      { x: "01-15", y: "64" },
      { x: "01-16", y: "121" },
      { x: "01-17", y: "49" },
      { x: "01-18", y: "81" },
      { x: "01-19", y: "49" },
    ],
  },
];

const objOne = arrayOne[0].data.reduce((aggObj, item) => {
  aggObj[item.x] = {...item, y: null};  
  return aggObj;
}, {});

const mergedObjOutput = arrayTwo[0].data.reduce((aggObj, item) => {
  aggObj[item.x] = item;  
  return aggObj;
}, objOne)

const mergedFinalOutput = [...arrayTwo];
mergedFinalOutput[0].data = Object.values(mergedObjOutput);

console.log(mergedFinalOutput);
.as-console-wrapper { max-height: 100% !important; top: 0; }


1

尝试这样做:

循环遍历元素,如果它们在第一个数组中没有找到,则将它们添加到第二个数组中。

const arrayOne = [
  {
    id: "This Year",
    data: [
      { x: "01-02", y: "81" },
      { x: "01-03", y: "361" },
      { x: "01-04", y: "64" },
      { x: "01-05", y: "169" },
      { x: "01-06", y: "9" },
      { x: "01-07", y: "100" },
      { x: "01-08", y: "144" },
      { x: "01-09", y: "81" },
      { x: "01-10", y: "256" },
      { x: "01-11", y: "81" },
      { x: "01-12", y: "144" },
      { x: "01-13", y: "144" },
      { x: "01-14", y: "225" },
      { x: "01-15", y: "289" },
      { x: "01-16", y: "81" },
      { x: "01-17", y: "64" },
      { x: "01-18", y: "64" },
      { x: "01-19", y: "121" },
      { x: "01-20", y: "25" },
      { x: "01-21", y: "49" },
      { x: "01-22", y: "16" },
      { x: "01-23", y: "49" },
      { x: "01-24", y: "196" },
      { x: "01-25", y: "16" },
      { x: "01-26", y: "25" },
      { x: "01-27", y: null },
      { x: "01-28", y: "144" },
      { x: "01-29", y: "100" },
      { x: "01-30", y: "64" },
      { x: "01-31", y: "144" },
      { x: "02-01", y: "100" },
      { x: "02-02", y: "100" },
      { x: "02-03", y: "49" }
    ]
  }
];

const arrayTwo = [
  {
    id: "This Year",
    data: [
      { x: "01-02", y: "64" },
      { x: "01-03", y: "25" },
      { x: "01-04", y: "25" },
      { x: "01-05", y: "169" },
      { x: "01-15", y: "64" },
      { x: "01-16", y: "121" },
      { x: "01-999", y: "49" },
      { x: "01-18", y: "81" },
      { x: "01-19", y: "49" }
    ]
  }
];

console.log(arrayOne);
arrayTwo[0].data.forEach(obj => {
  const found = arrayOne[0].data.find(obj2 => obj2.x === obj.x);
  if (!found) {
    arrayOne[0].data.push(obj);
  }
});

console.log(arrayOne);


从问题@Rizwan Ahmed Shivalli说“我想检查arrayTwo [data]中是否缺少数据,如果缺少,则从arrayOne [data]添加该数据”,但您的输出中有以下内容:“{”x“:”01-19“,”y“:”121“}”,这显然来自于“arrayOne”,而在arrayTwo中存在以下内容:“{”x“:”01-19“,”y“:”49“}”,因此(如果我正确理解问题)应该出现在输出中。 - Alex L

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接