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



>我有两个大数组,我想比较两个数组并将缺失的数据从 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}
]
}
]

你可以这样做:

  • 使用reduce从arrayOne[0].data创建对象(objOne(
  • 如果键存在于arrayTwo[0].data中,则覆盖objOne的任何属性,再次使用reduce并使用objOne作为初始值。
  • 将此objOne转换为具有Object.values(objOne)的数组,然后将其设置为arrayTwo[0].data的属性

对于两个归约函数,时间复杂度应为 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; }

尝试这样的事情:

遍历元素,如果在第一个数组中找不到它们,请将它们添加到第二个数组中

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);

最新更新