My task at hand involves merging two arrays of objects retrieved from different REST API calls. The structure of the arrays is as follows: [{date: date, name: name}, ...]
. Let's refer to them as Array A and Array B.
If both Array A and Array B contain objects with the same date, the final array should look like this:
[{date: date, nameA: nameA, nameB: nameB}]
If there are no matching dates, simply insert an object like this: [{date: dateA, nameA: nameA}]
For example:
Array A = [
{
date: 2017-01-01,
name: 'New Year Eve'
},
{
date: 2017-02-02,
name: 'feb2'
}
]
Array B = [
{
date: 2017-01-01,
name: 'New Year Eve'
},
{
date: 2017-03-03,
name: 'march3'
}
]
The final merged array would be:
finalArray = [{
date: 2017 - 01 - 01,
nameA: 'New Year Eve',
nameB: 'New Year Eve'
},
{
date: 2017 - 02 - 02,
nameA: 'feb2'
},
{
date: 2017 - 03 - 03,
nameB: 'march3'
}
]In this scenario, objects with the same date may appear in different positions within the array, making simple checks like:
arrayA[0].date === arrayB[0].date