Two arrays are at hand
const arrayOne = [
{id: '110'},
{id: '202'},
{id: '259'}
];
const arrayTwo = [
{data: [{value: 'Alpha', id: '001'}]},
{data: [{value: 'Bravo', id: '202'}]},
{data: [{value: 'Charlie', id: '752'}]},
{data: [{value: 'Delta', id: '202'}, {value: 'Sierra', id: '110'}]},
{data: [{value: 'Echo', id: '937'}]}
];
The task is to generate a new array by comparing arrayOne[idx].id
with arrayTwo[idx].data[idx2].id
If there is a match, the new array should contain either the value
or the entire matching object.
Using this example, the expected result would be
newArray = ['Bravo', 'Delta', 'Sierra']
My attempts so far include:
let result = arrayOne.map(el => {
let found = arrayTwo.find(f => f.data.at(0)?.id == el.id)?.data.at(0)?.value;
return { id: el.id, value: found ?? null};
});
const result = arrayTwo
.map(obj => obj.data[0])
.map(obj => (arrayOne.find(v => v.id === obj.id) && obj.value))
arrayOne.map(item => ({
...item,
result: arrayTwo.filter(itemTwo => item.data.map(x => x.id).includes(itemTwo.id))
}));