I have two different arrays of objects and need them to be combined by specific value of field.
array1 =
[
{ id: "id7", dateCreated: "2017-01-24", client: "Clipper"},
{ id: "id2", dateCreated: "2017-01-22", client: "Sniper" },
{ id: "id3", dateCreated: "2017-02-22", client: "Beeper" }
{ id: "id1", dateCreated: "2017-01-29", client: "Reaper" }
]
array2 =
[
{ id: "id2", dateExpired: "2020-01-24"},
{ id: "id1", dateExpired: "2020-01-22"}
]
I am expecting to get something like that in the end:
newData =
[
{ id: "id7", dateCreated: "2017-01-24", client: "Clipper"},
{ id: "id2", dateCreated: "2017-01-22", client: "Sniper", dateExpired: "2020-01-24" },
{ id: "id3", dateCreated: "2017-02-22", client: "Beeper" }
{ id: "id1", dateCreated: "2017-01-29", client: "Reaper", dateExpired: "2020-01-22" }
]
There are lots of fields in each and they are not sorted so I have to find the proper array2 object first and then combine them.
I have tried the following and unfortunately for some reason it either not reach the if statement or don't push new lines to newData.
let newData = [];
array1.forEach((item, row) => {
let find = array2.find((element) => {
return item.id===element.id
});
if (find) newData.push(Object.assign({},item,find));
});
return item.id=element.idThe equality operators in JavaScript/TypeScript are==and===.=is the assignment operator.