I have two big arrays I want to compare both arrays and add missing data from arrayOne to arrayTwo
this is my some of the data
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" },
],
},
];
I have tried to map it and compare it with respect to x but i am not able to achieve desirable output
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 })
}
});
});
I want to check if data is missing from arrayTwo[data] if it is missing then add that data from arrayOne[data] (i.e. take the object with its x-value but set the y-value to null)
Desired output:
[
{
"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}
]
}
]
{"x": "01-19", "y": "121"}from arrayOne, or do you want the following which exists in arrayTwo{"x": "01-19", "y": "49"}? I believe the latter from reading your question and @EugenSunic's answer is doing the opposite (would be an easy fix to reverse the behaviour)