如何在数组中建立套接字响应?

问题描述 投票:1回答:1

我有一个套接字返回给我:

个别更新示例(蓝线):

{
  "listBeans": [
    {
      "fruit": {
        "value": "apple"
      },
      "currentQuantity": {
        "value": "1"
      },
      "type": {
        "value": "B"
      },
      "price": {
        "value": "3.1"
      }
    }
  ]
}

历史记录样本(红线):

{
  "listBeans": [
    {
      "fruit": {
        "value": "apple"
      },
      "currentQuantity": {
        "value": "1"
      },
      "type": {
        "value": "A"
      },
      "price": {
        "value": "3.1"
      }
    },
    {
      "fruit": {
        "value": "banana"
      },
      "currentQuantity": {
        "value": "4"
      },
      "type": {
        "value": "A"
      },
      "price": {
        "value": "7.1"
      }
    }
  ]
}

我想检查我的列表中是否已经存在这些套接字返回之一,如果存在,我想更改那些值,如果不存在,我想我的清单中的concat

事实:我的套接字的第一个响应始终是水果的历史记录,之后的下一个响应将是水果的更新。

红线表示历史记录,蓝线表示个别更新socket

如果是更新或历史记录,则不可能有差异

第一条红线:A类型的历史记录

第二条红线:B类型的历史记录

这是我的意思:

if (response['listBeans'].length > 1) { // only works if my history returns more than one 
  this.myList = 
  this.myList.concat(this.wsFormatter.formatResponse(response['listBeans'])); // will just format my response
  this.myList = this.myList.filter(f => f.currentQuantity !== '0'); // i want to display only fruits with quantity bigger than 1
  this.myList = this.formatList(this.myList); // this function is important, they will add some keys to my object via Rest API, this function only need to be called when is a new fruit in my list
} else {
for (const item of this.listPositions) {
    if (response['listBeans'][0].fruit.value === item.fruit) { // here i verify i the response is the same as my list
        item.price = response['listBeans'][0].price.value; // change the value of price in my list
        item.currentQuantity = response['listBeans'][0].currentQuantity.value; // change the value of currentQuantity in my list
     }
   }
 }

我如何在列表(数组)中合并新的响应?我的代码仅在我的历史记录大于一时才有效,如果我的历史记录响应为一,则我的代码不起作用

这是我想要的:

enter image description here

typescript websocket concat
1个回答
0
投票

使用findIndex()查找history中是否已存在该项目。如果存在,则更新项目,否则添加项目。

let history = { "listBeans": [{ "fruit": { "value": "apple" }, "currentQuantity": { "value": "1" }, "type": { "value": "A" }, "price": { "value": "3.1" } }, { "fruit": { "value": "banana" }, "currentQuantity": { "value": "4" }, "type": { "value": "A" }, "price": { "value": "7.1" } } ] };

let update = { "listBeans": [{ "fruit": { "value": "apple" }, "currentQuantity": { "value": "1" }, "type": { "value": "B" }, "price": { "value": "3.1" } }] };

function updateData(history, update) {
  update.listBeans.forEach(obj => {
    let index = history.listBeans.findIndex(item => item.fruit.value === obj.fruit.value);

    // Update item if already exists.
    if (index != -1) {
      history.listBeans[index] = { ...obj };
    } else {
      // Append item.
      history.listBeans.push({ ...obj });
    }
  });

  return history;
}

console.log(updateData(history, update));

如果是更新或历史记录,则不可能有所不同。

在这种情况下,假设您具有全局变量history,则可以执行如下所示的操作。确保将history初始化为空数组。

// Incoming socket data.
let current = response; 

// Update history.
this.history = updateData(this.history, current);
© www.soinside.com 2019 - 2024. All rights reserved.