使用javascript从实时数据更新json数组

时间:2018-05-14 10:43:42

标签: javascript jquery json

我首先通过api请求获得一个非常大的json数组。然后使用signalR我得到一个较小的json数组,其中只包含已从api数据中更改过的json对象。

我要做的是从api数组中获取更改的对象,并使用signalR中的新数据更新它们。
以下是api回复。

[
  {carID: 38697, carNumber: 5665, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtQ=", postingID: 91},
  {carID: 38753, carNumber: 5522, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXtM=", postingID: 91},
  {carID: 38635, carNumber: 5830, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtI=", postingID: 91},
  {carID: 39340, carNumber: 30027, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtE=", postingID: 91},
  {carID: 39168, carNumber: 16064, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtA=", postingID: 91},
  {carID: 40107, carNumber: 5895, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXs8=", postingID: 91},
  {carID: 38647, carNumber: 22185, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXs4=", postingID: 91},
  {carID: 39416, carNumber: 22180, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXs0=", postingID: 91}
]

这是signalR更新。

[
  {carID: 38697, carNumber: 5665, dispatchStatus: 1, dataVersionNr: "AAAAAAhFXtQ=", postingID: 42},
  {carID: 38753, carNumber: 5522, dispatchStatus: 3, dataVersionNr: "AAAAAAhFXtM=", postingID: 91},
]

carNumber是独一无二的。

2 个答案:

答案 0 :(得分:2)

您可以使用Object.assign方法更新您的给定数组。另外,通过传递map函数作为参数来使用callback方法,该函数应用于给定数组中的每个项目。



let array = [ {carID: 38697, carNumber: 5665, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtQ=", postingID: 91}, {carID: 38753, carNumber: 5522, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXtM=", postingID: 91}, {carID: 38635, carNumber: 5830, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtI=", postingID: 91}, {carID: 39340, carNumber: 30027, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtE=", postingID: 91}, {carID: 39168, carNumber: 16064, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtA=", postingID: 91}, {carID: 40107, carNumber: 5895, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXs8=", postingID: 91}, {carID: 38647, carNumber: 22185, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXs4=", postingID: 91}, {carID: 39416, carNumber: 22180, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXs0=", postingID: 91} ]

let update_array = [ {carID: 38697, carNumber: 5665, dispatchStatus: 1, dataVersionNr: "AAAAAAhFXtQ=", postingID: 42}, {carID: 38753, carNumber: 5522, dispatchStatus: 3, dataVersionNr: "AAAAAAhFXtM=", postingID: 91}, ]

array = array.map(function(item){
  var car = update_array.find(a => a.carID == item.carID);
  return car ? Object.assign(item, car) : item;
});
console.log(array);




答案 1 :(得分:2)

您可以循环访问更新并使用Object.assign进行更新:

var datas = [
{carID: 38697, carNumber: 5665, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtQ=", postingID: 91},
{carID: 38753, carNumber: 5522, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXtM=", postingID: 91},
{carID: 38635, carNumber: 5830, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtI=", postingID: 91},
{carID: 39340, carNumber: 30027, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtE=", postingID: 91},
{carID: 39168, carNumber: 16064, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXtA=", postingID: 91},
{carID: 40107, carNumber: 5895, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXs8=", postingID: 91},
{carID: 38647, carNumber: 22185, dispatchStatus: 0, dataVersionNr: "AAAAAAhFXs4=", postingID: 91},
{carID: 39416, carNumber: 22180, dispatchStatus: 2, dataVersionNr: "AAAAAAhFXs0=", postingID: 91}
];

var updates = [
{carID: 38697, carNumber: 5665, dispatchStatus: 1, dataVersionNr: "AAAAAAhFXtQ=", postingID: 42},
{carID: 38753, carNumber: 5522, dispatchStatus: 3, dataVersionNr: "AAAAAAhFXtM=", postingID: 91},
];

updates.forEach(u => {
  var found = datas.find(d => d.carID === u.carID);
  found && Object.assign(found, u);
})

console.log(datas);