检查对象数组中是否存在对象 ID

Check if object ID exists in Object array

我从 sessionStorage 中解析了这个对象数组。我想在数组中添加一个项目,但如果它已经存在,则只更新项目的数量而不是再次添加它。到目前为止,这是我尝试过的方法:

    var tobeSaved = store.getters.printCart; //This is the data that I want to add

    if(tobeSaved.length){   
      if(sessionStorage.length){
        var existingCart = JSON.parse(sessionStorage.cart); //This is the sessionStorage items
        var size = Object.keys(existingCart).length;    

        const found = tobeSaved.some(el => el.itemID === existingCart.itemID);
        if(found){
          
        } else{
          existingCart.push(tobeSaved[0]);  

          let myObj_serialized = JSON.stringify(existingCart);  
          sessionStorage.setItem('cart', myObj_serialized);
        }
      } else{   
        let myObj_serialized = JSON.stringify(tobeSaved);   
        sessionStorage.setItem('cart', myObj_serialized);   
      } 

这是我的 SessionStorage 数组:

    0:
      itemID: "11111"
      itemName: "sessionItem1"
      itemPrice: 550
      itemQuantity: 1
    1:
      itemID: "22222"
      itemName: "sessionItem2"
      itemPrice: 550
      itemQuantity: 1

这是我的 tobeSaved 对象:

itemID: "11111",
itemName: "sessionItem1"
itemPrice: 550
itemQuantity: 6

我想做的是,如果 tobeSaved 已经存在于 existingCart 中,它应该只更新数量,如果不存在,则在数组中添加另一个对象。

这应该适合你。 您正在遍历 item ,理想情况下您应该遍历现有数组并检查 ID 是否存在?

const found = tobeSaved.some(el => el.itemID === existingCart.itemID);

应该是

const found = existingArray.some(el => el.itemID === toBeSaved.itemID);

或者像我一样使用 find ,这对我来说似乎是个不错的选择。

let obj = exisitngArr.find(o => o.ID === newObj.ID);


var arr=[{
          itemID: "11111",
          itemName: "sessionItem1",
          itemPrice: 550,
          itemQuantity: 1
    },
         {
          itemID: "22222",
          itemName: "sessionItem2",
          itemPrice: 550,
          itemQuantity: 1
    }];
    
    window.sessionStorage.setItem('cart',JSON.stringify(arr));
    
    var exisitngArr = JSON.parse(window.sessionStorage.getItem('cart'));
    
    var newObj = {itemID: "11111",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 6}
    
    function updateData(){
    let obj = exisitngArr.find(o => o.ID === newObj.ID);    
    
        if(obj!== undefined && obj !== null){
            obj.itemQuantity = newObj.itemQuantity;    
        } else {
            exisitngArr.push(newObj);
        }
    window.sessionStorage.setItem('cart',JSON.stringify(exisitngArr));
    }
    
    updateData();
    
    console.log(JSON.parse(window.sessionStorage.getItem('cart')));

或者这个怎​​么样?

const SessionStorage = [
  {
    itemID: "11111",
    itemName: "sessionItem1",
    itemPrice: 550,
    itemQuantity: 1,
  },
  {
    itemID: "22222",
    itemName: "sessionItem2",
    itemPrice: 550,
    itemQuantity: 1,
  }
];

const toBeSaved = {
  itemID: "11111",
  itemName: "sessionItem1",
  itemPrice: 550,
  itemQuantity: 6,
}

const addOrUpdate = (SessionStorageArray, toBeSavedObject) => {

  const index = SessionStorageArray.findIndex(session => session.itemID === toBeSavedObject.itemID);
  
  if (index === -1) {
    SessionStorageArray.push(toBeSavedObject);
  } else {
    SessionStorageArray[index].itemQuantity = toBeSavedObject.itemQuantity;
  }
  
  return SessionStorageArray;
};

console.table(addOrUpdate(SessionStorage, toBeSaved));