SessionStorage 复制具有相同 ID 的对象

SessionStorage duplicates object with the same ID

我目前正在使用 vue.js + sessionStorage 制作一个简单的购物车。这是我的 js 代码(vue.js 方法):

storeLocalStorage(){
    var tempitem = store.getters.printCart;

    if(tempitem.length){
      if(sessionStorage.length){
        var existingCart = JSON.parse(sessionStorage.cart);
        var size = Object.keys(existingCart).length;
        //console.log(existingCart);

        const index = existingCart.findIndex(existingCart => existingCart.itemID === tempitem.itemID);
        //console.log(index);

        if (index === -1) {
          existingCart.push(tempitem[0]);
        } else {
          existingCart[index].itemQuantity = existingCart[index].itemQuantity + tempitem.itemQuantity;
        }
        
        let myObj_serialized = JSON.stringify(existingCart);
        sessionStorage.setItem('cart', myObj_serialized); 
      } 
      else {
        let myObj_serialized = JSON.stringify(tempitem);
        sessionStorage.setItem('cart', myObj_serialized);
      }
    }
  }

这是我的对象数组:

临时项目:

    {
    itemID: "a1b1c1",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 1,
  },
  {
    itemID: "a2b2c2",
    itemName: "sessionItem2",
    itemPrice: 550,
    itemQuantity: 1,
  }

sessionStorage 项目:

  0:
    itemID: "a1b1c1",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 1,
  1:
    itemID: "a3b3c3",
    itemName: "sessionItem3",
    itemPrice: 550,
    itemQuantity: 1,
  2:
    itemID: "a2b2c2",
    itemName: "sessionItem2",
    itemPrice: 550,
    itemQuantity: 1,
 

我想要完成的是,每当一个tempitem对象与一个sessionStorage项目有相同的ID时,它们的数量将被加起来并传递给sessionStorage项目,但如果没有,它会在中添加另一个索引会话存储。但是发生的事情是,它没有将两个数量相加,而是创建了另一个具有相同项目但数量不同的索引。

0:
    itemID: "a1b1c1",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 1,
  1:
    itemID: "a1b1c1",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 5,

我该如何完成?

You can use this logic to sum the quantities , rest all remains same. Loop over the new array items, check for the object in existing array , if it exists add the quantity else add the new object in the existing array.

var arr = [{
    itemID: "a1b1c1",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 1,
},{
    itemID: "a3b3c3",
    itemName: "sessionItem3",
    itemPrice: 550,
    itemQuantity: 1},
           {
    itemID: "a2b2c2",
    itemName: "sessionItem2",
    itemPrice: 550,
    itemQuantity: 1}];

// new items array
var newArr= [{
    itemID: "a1b1c1",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 1},
             {
    itemID: "a1b1c1",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 5},
{
    itemID: "a5b5c5",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 5}]


newArr.forEach(newEle => {
    let obj = arr.find(ob => ob.itemID === newEle.itemID);
  if(obj!==undefined){
     obj.itemQuantity+=newEle.itemQuantity;
  } else{
    arr.push(newEle);
    }

})

    console.log(arr);