I am attempting to locate an element in the target array and update it in the source array.
let sourceArray = [
{
"userId": "123",
"applicationId": "abc",
"selections": [
{
"applicationId": 70930,
"creationDate": "2021-01-28",
"responseStatus": "PENDING"
}
]
}
]
let array2 = [
{
"applicationId": 70930,
"code": "TEST CODE",
"creationDate": "2021-01-28",
"submissionDate": "2021-01-29",
"status": "SUBMITTED",
"outcomeStatus": "PENDING",
"responseStatus": "PENDING"
}
]
The desired outcome is for selections in the source array to be updated with matching elements from array2 based on applicationId
[
{
"userId": "123",
"applicationId": "abc",
"selections": [
{
"applicationId": 70930,
"code": "TEST CODE",
"creationDate": "2021-01-28",
"submissionDate": "2021-01-29",
"status": "SUBMITTED",
"outcomeStatus": "PENDING",
"responseStatus": "PENDING"
}
]
}
]
I attempted updating the array using the following code snippet
const newArray = sourceArray.map(item => {
let item2 = array2.find(i2 => item.selections.some(id => i2.applicationId === id.applicationId));
return item2 ? { ...item, ...item2 } : item;
});