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