-1

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);
                        }
                        
                       
                    }
                
                  }
  • Perhaps use `find` instead of `some`. If `undefined` is returned it is not in the productList so push the object to productList. ELSE the found object is returned, just add one to `quantity` – radarbob Oct 12 '21 at 18:52
  • Better not use the array structure then, but a plain object, keyed by `Id`. – trincot Oct 12 '21 at 18:53

2 Answers2

1

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

...but trying to stick with the array is a guarantee for inefficient code. It just isn't the right tool for the job here. You should adapt your code to work with the plain object representation throughout. You can iterate over the values in an object, you can remove items from an object, update, add, ...etc.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • sir how do i keep those two updated objects because i'm doing this operation inside onchange Function – starboy The Oct 13 '21 at 05:15
  • Preferably you should adapt your code to work only with the object. An array is really not the right data structure, and your code will be inefficient if somehow you try to stick with the array and keep things unique. You can iterate over an object, so there should really be no good reason to want an array here. – trincot Oct 13 '21 at 05:17
1

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 } }
Erik
  • 465
  • 3
  • 11
  • I tried your approach but myMap i need to push back into list but again it will add one more entries newProductList.push(myMap.values()) – starboy The Oct 12 '21 at 20:20
  • Why do you need to push the map values into an array? I would rather keep the map and then [iterate over the map](https://hackinbits.com/articles/how-to-iterate-a-map-in-javascript---map-part-2) when I need it: – Erik Oct 13 '21 at 05:01