I am trying to combine two object based on keys given But, It become hard then expected.
const keys = ["year_and_month", "name"];
const firstData = [
{
"year_and_month": "2022-01",
"name": "Roy",
"data1": 3388560126.065652,
"data2": 889386921115.6444
},
{
"year_and_month": "2022-02",
"name": "Roy",
"data1": 3897475493.9987683,
"data2": 874308539354.9275
},
{
"year_and_month": "2022-02",
"name": "Dan",
"data1": 443961561.24720746,
"data2": 293742072318.20105
},
{
"year_and_month": "2022-01",
"name": "Dan",
"data1": 362316175.165702,
"data2": 303232994015.44
},
{
"year_and_month": "2022-02",
"name": "Tata",
"data1": 22744562.245873,
"data2": 790044572051.9896
},
{
"year_and_month": "2022-01",
"name": "Tata",
"data1": 22744562.245873,
"data2": 790044572051.9896
}
]
const secondData = [
{
"year_and_month": "2022-01",
"name": "Roy",
"data3": 4083741657.64,
"data4": 36693935225.96
},
{
"year_and_month": "2022-02",
"name": "Roy",
"data3": 2436561209.83,
"data4": 31013728462.63
},
{
"year_and_month": "2022-02",
"name": "Dan",
"data3": -8298497218.39,
"data4": 34817072736.04
},
{
"year_and_month": "2022-01",
"name": "Dan",
"data3": 1312315161.48,
"data4": 34790053142.56
},
{
"year_and_month": "2022-01",
"name": "Tata",
"data3": -745507724.01,
"data4": 13533928780.38
},
{
"year_and_month": "2022-02",
"name": "Tata",
"data3": 658173707,
"data4": 13282740718.8
}
]
//This way, don't find the way to move forward
//let firstKeys = [];
//let secondKeys = [];
//firstData.map(data => keys.forEach(key => firstKeys.push(data[key]) ));
//secondData.map(data => keys.forEach(key => secondKeys.push(data[key]) ));
//console.log(firstKeys);
//console.log(secondKeys);
//Tried Second way But, stuck again
const result = secondData.reduce((res, ele) => {
console.log(ele);
keys.forEach(key => {
firstData.find(data => data[key] === ele[key]);
})
return res
}, [])
console.log("result", result);
Expected Result (combine both array based on keys given like here match year_and_month and name):
[{
"year_and_month": "2022-01",
"name": "Roy",
"data1": 3388560126.065652,
"data2": 889386921115.6444,
"data3": 4083741657.64,
"data4": 36693935225.96
},
{
"year_and_month": "2022-02",
"name": "Roy",
"data1": 3897475493.9987683,
"data2": 874308539354.9275,
"data3": -8298497218.39,
"data4": 34817072736.04
}
... and so on
]