Как добавить в объект только новые данные, удалив уже существующие?

Есть JSON, который хранится в store после запроса к серверу:
JSON
[{"trade_id":79303062,"type":"buy","quantity":"0.4697165","price":"4120","amount":"1935.23198","date":1546969734},{"trade_id":79303061,"type":"buy","quantity":"0.01","price":"4119.99999999","amount":"41.19999999","date":1546969734},{"trade_id":79303050,"type":"buy","quantity":"0.01","price":"4106.12726754","amount":"41.06127267","date":1546969721},{"trade_id":79303036,"type":"buy","quantity":"0.0002353","price":"4106.10673649","amount":"0.96616691","date":1546969704},{"trade_id":79303034,"type":"buy","quantity":"0.0097647","price":"4106.10673649","amount":"40.09490044","date":1546969703},{"trade_id":79302996,"type":"buy","quantity":"0.00023562","price":"4106.10673667","amount":"0.96748086","date":1546969674},{"trade_id":79302992,"type":"buy","quantity":"0.00976438","price":"4106.10673667","amount":"40.09358649","date":1546969673},{"trade_id":79302948,"type":"buy","quantity":"0.01","price":"4106.12726696","amount":"41.06127266","date":1546969631},{"trade_id":79302916,"type":"buy","quantity":"0.0059819","price":"4120","amount":"24.645428","date":1546969582},{"trade_id":79302896,"type":"buy","quantity":"0.01","price":"4105.0205253","amount":"41.05020525","date":1546969573},{"trade_id":79302890,"type":"buy","quantity":"2.2","price":"4116.4","amount":"9056.08","date":1546969566},{"trade_id":79302825,"type":"buy","quantity":"0.00130589","price":"4120","amount":"5.3802668","date":1546969495},{"trade_id":79302823,"type":"buy","quantity":"0.00130589","price":"4120","amount":"5.3802668","date":1546969494},{"trade_id":79302822,"type":"buy","quantity":"0.00130589","price":"4120","amount":"5.3802668","date":1546969493},{"trade_id":79302694,"type":"buy","quantity":"0.11771911","price":"4128.71043616","amount":"486.02811799","date":1546969407},{"trade_id":79302693,"type":"buy","quantity":"0.00526773","price":"4125","amount":"21.72938625","date":1546969407},{"trade_id":79302692,"type":"buy","quantity":"0.01919635","price":"4119.99999999","amount":"79.08896199","date":1546969407},{"trade_id":79302625,"type":"buy","quantity":"0.00473969","price":"4110.00010001","amount":"19.48012637","date":1546969386},{"trade_id":79302614,"type":"buy","quantity":"0.0243309","price":"4110.0001","amount":"100.00000143","date":1546969383},{"trade_id":79302545,"type":"buy","quantity":"0.00126099","price":"4111","amount":"5.18392989","date":1546969319},{"trade_id":79302406,"type":"buy","quantity":"0.00144747","price":"4120","amount":"5.9635764","date":1546969241},{"trade_id":79302364,"type":"buy","quantity":"0.00651364","price":"4120","amount":"26.8361968","date":1546969165},{"trade_id":79302256,"type":"buy","quantity":"0.00513864","price":"4120","amount":"21.1711968","date":1546969000},{"trade_id":79302254,"type":"buy","quantity":"0.00156201","price":"4120","amount":"6.4354812","date":1546968999},{"trade_id":79302241,"type":"buy","quantity":"0.01495102","price":"4120","amount":"61.5982024","date":1546968987},{"trade_id":79302240,"type":"buy","quantity":"0.01510339","price":"4120","amount":"62.2259668","date":1546968987},{"trade_id":79302233,"type":"buy","quantity":"0.00160357","price":"4120","amount":"6.6067084","date":1546968975}]


Раз в минуту запрашиваются новые данные, которые уже могут содержать хранящиеся у меня данные, как можно не очень громоздко отфильтровать/вычесть/что-нибудь еще, полученный массив из запроса и добавить только те записи, чей trade_id не находится в ,уже хранящихся у меня, данных?
  • Вопрос задан
  • 101 просмотр
Решения вопроса 1
0xD34F
@0xD34F
items.push(...newItems.filter(n => !items.find(m => m.trade_id === n.trade_id)))
Ответ написан
Пригласить эксперта
Ответы на вопрос 2
teknik2008
@teknik2008
Расскажите про GOLANG. Мне интересно
Я бы построил индекс из массива, где ключ trade_id, а значение его положение в массиве.
Потом перебрал новый массив, все записи которые есть в индексе обновил. если нет в индексе добавил.
Тогда не надо будет на каждой итерации искать записи по зависимому массиву
spoiler
let oldRows = [{"trade_id":79303062,"type":"buy","quantity":"0.4697165","price":"4120","amount":"1935.23198","date":1546969734},{"trade_id":79303061,"type":"buy","quantity":"0.01","price":"4119.99999999","amount":"41.19999999","date":1546969734},{"trade_id":79303050,"type":"buy","quantity":"0.01","price":"4106.12726754","amount":"41.06127267","date":1546969721},{"trade_id":79303036,"type":"buy","quantity":"0.0002353","price":"4106.10673649","amount":"0.96616691","date":1546969704},{"trade_id":79303034,"type":"buy","quantity":"0.0097647","price":"4106.10673649","amount":"40.09490044","date":1546969703},{"trade_id":79302996,"type":"buy","quantity":"0.00023562","price":"4106.10673667","amount":"0.96748086","date":1546969674},{"trade_id":79302992,"type":"buy","quantity":"0.00976438","price":"4106.10673667","amount":"40.09358649","date":1546969673},{"trade_id":79302948,"type":"buy","quantity":"0.01","price":"4106.12726696","amount":"41.06127266","date":1546969631},{"trade_id":79302916,"type":"buy","quantity":"0.0059819","price":"4120","amount":"24.645428","date":1546969582},{"trade_id":79302896,"type":"buy","quantity":"0.01","price":"4105.0205253","amount":"41.05020525","date":1546969573},{"trade_id":79302890,"type":"buy","quantity":"2.2","price":"4116.4","amount":"9056.08","date":1546969566},{"trade_id":79302825,"type":"buy","quantity":"0.00130589","price":"4120","amount":"5.3802668","date":1546969495},{"trade_id":79302823,"type":"buy","quantity":"0.00130589","price":"4120","amount":"5.3802668","date":1546969494},{"trade_id":79302822,"type":"buy","quantity":"0.00130589","price":"4120","amount":"5.3802668","date":1546969493},{"trade_id":79302694,"type":"buy","quantity":"0.11771911","price":"4128.71043616","amount":"486.02811799","date":1546969407},{"trade_id":79302693,"type":"buy","quantity":"0.00526773","price":"4125","amount":"21.72938625","date":1546969407},{"trade_id":79302692,"type":"buy","quantity":"0.01919635","price":"4119.99999999","amount":"79.08896199","date":1546969407},{"trade_id":79302625,"type":"buy","quantity":"0.00473969","price":"4110.00010001","amount":"19.48012637","date":1546969386},{"trade_id":79302614,"type":"buy","quantity":"0.0243309","price":"4110.0001","amount":"100.00000143","date":1546969383},{"trade_id":79302545,"type":"buy","quantity":"0.00126099","price":"4111","amount":"5.18392989","date":1546969319},{"trade_id":79302406,"type":"buy","quantity":"0.00144747","price":"4120","amount":"5.9635764","date":1546969241},{"trade_id":79302364,"type":"buy","quantity":"0.00651364","price":"4120","amount":"26.8361968","date":1546969165},{"trade_id":79302256,"type":"buy","quantity":"0.00513864","price":"4120","amount":"21.1711968","date":1546969000},{"trade_id":79302254,"type":"buy","quantity":"0.00156201","price":"4120","amount":"6.4354812","date":1546968999},{"trade_id":79302241,"type":"buy","quantity":"0.01495102","price":"4120","amount":"61.5982024","date":1546968987},{"trade_id":79302240,"type":"buy","quantity":"0.01510339","price":"4120","amount":"62.2259668","date":1546968987},{"trade_id":79302233,"type":"buy","quantity":"0.00160357","price":"4120","amount":"6.6067084","date":1546968975}]

let newRows = [
  {"trade_id":79303063,"type":"buy","quantity":"0.4697165","price":"4120","amount":"1935.23198","date":1546969734},
  {"trade_id":79303061,"type":"buy","quantity":"0.123132","price":"2345","amount":"1935.5213123","date":1546969734},
]


function createIndex(data, key){
  return data.reduce((index, row, i)=>{
    let indexKey = row[key];
    index[indexKey] = i;
    return index;
  }, {})
}

function mergeData(oldRows, newRows, key) {
  let index = createIndex(oldRows, key);
  newRows.forEach(row =>{
    let keyValue = row[key];
    if(keyValue in index){
      let i = index[keyValue];
      oldRows[i] = Object.assign(oldRows[i], row);
    }else{
      oldRows.push(row)
    }
  })
  return oldRows
}

let data =  mergeData(oldRows, newRows, 'trade_id')
console.log(data)

Ответ написан
@Guccigang
Использование Map вы вообще не рассматниваете? По-моему именно то что нужно в описанной ситуации.

https://developer.mozilla.org/en-US/docs/Web/JavaS...
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
IBC Human Resources Екатеринбург
от 80 000 руб.
Apsis Ростов-на-Дону
от 85 000 руб.
26 марта 2019, в 22:12
30000 руб./за проект
26 марта 2019, в 20:35
10000 руб./за проект