Home > Back-end >  Pushing array without duplicate and change the property value of object in Javascript?
Pushing array without duplicate and change the property value of object in Javascript?

Time:10-13

I have an object and i'm pushing into array while pushing i need to restrict the duplicate and also if there is any changes in the property then update the array accordingly ?

    [{
    Id: "a134",
    Name: "Name -",
    Company: "001",
    Product :"01"
    quantity :1
    },
   {
    Id: "a135",
    Name: "Name -1",
    Company: "002",
    Product :"03"
    quantity :2  -----> (Previous event.target.name)
    },
    {
    Id: "a135",
    Name: "Name -1",
    Company: "002",
    Product :"03"
    quantity :3  ---> (current event.target.name)
    }
  ]

if i'm pushing into array there might be chance to update quantity how to achieve the below result

[{
        Id: "a134",
        Name: "Name -",
        Company: "001",
        Product :"01"
        quantity :1
        },
        {
        Id: "a135",
        Name: "Name -1",
        Company: "002",
        Product :"03"
        quantity :3  ---> (current event.target.name)
        }
      ]

and now my code is

 if(event.target.value!='')
                  {
                  
                    const searchObj = this.myList.find(({ Id,Product, Company  }) => Product === index);
                    if (searchObj)
                    {
                        console.log('searchObj',searchObj);
                       
                       resultVal = { ...searchObj, quantity:parseInt(event.target.value)}; 
                       
                       if (!this.newProductList.some(e => e.Product === resultVal.Product)) 
                        {
                         this.newProductList.push(resultVal);
                        }
                        
                       
                    }
                
                  }

CodePudding user response:

This is best solved by using a different data structure: use a plain object, keyed by Id:

let data = {
    "a134": {
        Id: "a134"
        Name: "Name -",
        Company: "001",
        Product: "01"
        quantity: 1

    },
    "a135": {
        Id: "a135",
        Name: "Name -1",
        Company: "002",
        Product: "03"
        quantity: 2
    },
};

To update/add don't push, but set the object key's value. For example:

// Let's say we want to add/update with this object:
let objToAdd = {
    Id: "a135",
    Name: "Name -1",
    Company: "002",
    Product: "03"
    quantity: 3
}
// ...then just do:
data[objToAdd.Id] = objToAdd;

This will either update or "insert". In the above case, it will update:

let data = {
    "a134": {
        Id: "a134"
        Name: "Name -",
        Company: "001",
        Product: "01"
        quantity: 1
    },
    "a135": {
        Id: "a135",
        Name: "Name -1",
        Company: "002",
        Product: "03"
        quantity: 3
    },
};

If you ever need the array-format, then just do:

let arr = Object.values(data);

CodePudding user response:

I think an array is the wrong data type. I would prefer using a map.

This of course only works if the Id is unique since that is what is best used as key:

const myMap = new Map(["a134",{
                               Name: "Name -",
                               Company: "001",
                               Product :"01",
                               quantity :1
                              }]);


And then it is easy to check if an item is already present, before updating the quantity or adding a new item.

const id = "a134"; // or "a135" for a new entry
const newItem = { Name: "Name B",
                  Company: "002",
                  Product :"012",
                  quantity :1
                  }
if(myMap.has(id)){
  const item = myMap.get(id);
  myMap.set(id, {...item, quantity: item.quantity  1})
} else {
   myMap.set(id,newItem);
}
console.log(myMap) // Map {'a134' => { Name: 'Name A', Company: '001', Product:'01', quantity: 2 } }
  • Related