I have two Javascript arrays of objects with various keys/values.
I'm trying to achieve a new array of objects with selected keys/values from each of the original arrays, but have uniqueness on a specific key/value.
Example:
const startDate = [
{
name: 'John', //Don't need this
Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', //This should be the 'unique' key
datePosted: '2020-04-04T00:01:20.000Z' //This will be the start date
}
]
const endDate = [
{
name: 'James', //Don't need this
Id: 'ae570d88-809b-45b1-bc20-69b569e361ce', //This should be the 'unique' key
datePosted: '2021-04-04T00:01:20.000Z' //This will be the end date
}
]
const desiredOutput = [
{
'ae570d88-809b-45b1-bc20-69b569e361ce': {
startDate: '2020-04-04T00:01:20.000Z',
endDate: '2021-04-04T00:01:20.000Z'
}
}
]
const desiredOutput2 = [
{
Id: 'ae570d88-809b-45b1-bc20-69b569e361ce',
startDate: '2020-04-04T00:01:20.000Z',
endDate: '2021-04-04T00:01:20.000Z'
}
]
I've tried using the JS spread operator but can't figure out the renaming of the key to startDate/endDate and adding both to the same object within the array based on uniqueness of the 'Id' key.
Either of the two desiredOutputs would work great