简体   繁体   English

检查 Object 数组中是否存在 object ID

[英]Check if object ID exists in Object array

I have this object array parsed from sessionStorage.我从 sessionStorage 解析了这个 object 数组。 I want to add an item inside the array but if it already exists, only update the quantity of the item and not add it again.我想在数组中添加一个项目,但如果它已经存在,则只更新该项目的数量,而不是再次添加它。 Here's what I have tried so far:这是我到目前为止所尝试的:

    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);   
      } 

Here's my SessionStorage Array:这是我的 SessionStorage 数组:

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

And here's my tobeSaved object:这是我的 tobeSaved object:

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

What i want to do is if tobeSaved already exists inside existingCart, it should only update the quantity but if it doesn't, add another object in the array.我想要做的是,如果 tobeSaved 已经存在于现有购物车中,它应该只更新数量,但如果没有,则在数组中添加另一个 object。

This should work for you.这应该适合你。 Your are looping over the item, ideally you should be looping over the existing array and check if the ID exists or not?您正在遍历该项目,理想情况下您应该遍历现有数组并检查 ID 是否存在?

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

should be应该

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

or as I did using find, which seems to good option to me.或者就像我使用 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')));

Or how about this?或者这个怎么样?

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM