Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
Pushing array without duplicate and change the property value of object in Javascript?

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);
                        }
                        
                       
                    }
                
                  }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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 } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!