I am dealing with two distinct arrays. The first one looks like this:
const arrOne = [
[{sender: '0000', to: '1111'}],
[{sender: '2222', to: '1111'}]
];
And the second array is structured as follows:
const arrTwo = [
{
firstName: 'John',
lastName: 'Doe',
num: '1111'
}
]
My goal is to match the key to
from arrOne with the num
from arrTwo. If a match is found, I want to update the first array with the corresponding firstname and lastname. Therefore, the desired output should look like this:
const arrOne = [
[{sender: '0000', to: '1111', firstName: 'John', lastName: 'Doe'}],
[{sender: '2222', to: '1111', firstName: 'John', lastName: 'Doe'}]
];
This is what I have attempted so far:
arrOne = arrOne.map(item => {
const item2 = arrTwo.find(i2 => i2.num == item.to);
return item2 ? { ...item, ...item2 } : item;
});
Thank you in advance for your help. Could you also provide guidance on achieving this using TypeScript?