У меня есть функция с именем syncWithPreviousDay
, которая получает массив объектов ниже как свойство.
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"
}
]
Внутри этой функции мне нужно сравнить ключ URL
с другим массивом объектов из моей базы данных, который имеет те же пары ключей и где есть совпадение, переместите ключи Position
и CustomKey
на новый ключ PreviousPosition
и CustomKey
в data.chart
массив. Если совпадений нет, создайте значение null
для обеих клавиш.
const syncWithPreviousDay = (jsonObj) => {
const data = {
date: config.dateToday,
chart: jsonObj
}
dbService.getDate(config.yesterday)
.then( result => {
console.log(result.chart)
})
}
Результат из console.log
выглядит следующим образом:
[
{
"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
}
]
Итак, мой желаемый data.chart
как-то должно выглядеть так:
[
{
"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"
}
]
Как это можно сделать?