I have a function called syncWithPreviousDay that receives below array of object as a propery.
jsonObj
[
{
"Position": "1",
"TrackName": "Rocket",
"URL": "https://domain.local/nbs678"
},
{
"Position": "2",
"TrackName": "Dueling banjos",
"URL": "https://domain.local/asd456"
},
{
"Position": "3",
"TrackName": "One hit wonder",
"URL": "https://domain.local/xyz123"
}
]
Inside this function I need compare the key URL with another array of objects from my database that has the same key pairs and wherever there is a match, move the keys Position and CustomKey to new key PreviousPosition and CustomKey in the data.chart array. If there is no match, create a null value for both keys.
const syncWithPreviousDay = (jsonObj) => {
const data = {
date: config.dateToday,
chart: jsonObj
}
dbService.getDate(config.yesterday)
.then( result => {
console.log(result.chart)
})
}
Result from the console.log looks like this:
[
{
"Position": "1",
"TrackName": "One hit wonder",
"URL": "https://domain.local/xyz123",
"CustomKey": "x"
},
{
"Position": "2",
"TrackName": "Awesome old song",
"URL": "https://domain.local/123qwe",
"CustomKey": "y"
},
{
"Position": "3",
"TrackName": "Dueling banjos",
"URL": "https://domain.local/asd456",
"CustomKey": null
}
]
So my desired data.chart should somehow look like this:
[
{
"Position": "1",
"TrackName": "Rocket",
"URL": "https://domain.local/nbs678",
"PreviousPosition": null,
"CustomKey": null
},
{
"Position": "2",
"TrackName": "Dueling banjos",
"URL": "https://domain.local/asd456",
"PreviousPosition": "3",
"CustomKey": null
},
{
"Position": "3",
"TrackName": "One hit wonder",
"URL": "https://domain.local/xyz123",
"PreviousPosition": "1",
"CustomKey": "x"
}
]
How can this be done?