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

226
Views
Check duplicate value in localstorage

I want to check if a id exist in database it will just simply replace it. When i try to do that it is adding the same id.

addEntry = (e,id) => {
  e.preventDefault()
    
  let product_list = []
  let productCost = document.getElementById('projectcost').value;
  let productQty = document.getElementById('productQty'+id).value;
  let productId = id;
    
  if(localStorage.getItem('myItems')){
    product_list = JSON.parse(localStorage.getItem('myItems'))
    product_list.push({productId,productCost,productQty})
    localStorage.setItem('myItems',JSON.stringify(product_list))
  } else {
    product_list.push({productId,productCost,productQty})
    localStorage.setItem('myItems', JSON.stringify([{productId, productCost, productQty}]))
  }
}

Output

[{"productId":44,"productName":"Cleansing milk with Toner","productCost":"140","productQty":"1"},{"productId":44,"productName":"Cleansing milk with Toner","productCost":"280","productQty":"2"},{"productId":44,"productName":"Cleansing milk with Toner","productCost":"420","productQty":"3"}]

output I want

[{"productId":44,"productName":"Cleansing milk with Toner","productCost":"280","productQty":"2"},{"productId":43,"productName":"Hair Spa 100 gm","productCost":"160","productQty":"1"}]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you need to check if the product is already located in localStorage if so get it index and replace it and if not just append it to the product_list

try this

let product_list = []
  let productCost = document.getElementById('projectcost').value;
  let productQty = document.getElementById('productQty'+id).value;
  let productId = id;

  if(localStorage.getItem('myItems')){
    product_list = JSON.parse(localStorage.getItem('myItems'))
    const DuplicatedIndex = product_list.findIndex(product => product.productId == productId)

if(DuplicatedIndex == -1) {
   product_list.push({productId,productCost,productQty})
} else {
  product_list[DuplicatedIndex] = {productId,productCost,productQty}
}
localStorage.setItem('myItems',JSON.stringify(product_list))

   }else{
   
    product_list.push({productId,productCost,productQty})
    localStorage.setItem('myItems',JSON.stringify([{productId,productCost,productQty}]))
   }
}

about 4 years ago · Juan Pablo Isaza Report

0

Before pushing the new item into product_list, you should check if that list has the item whose productId equals to productId and then replace it with the new item or just push that item. I will put the correct code below:

const index = product_list.findIndex(item => item.productId === productId);
if(index === -1) {
    product_list.push({productId,productCost,productQty});
} else {
    product_list[index] = {productId,productCost,productQty};
}
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!