Maureen Moore

Maureen Moore

  • NA
  • 206
  • 0

Send alert if item has already been selected

Aug 5 2020 6:01 PM
  1. public onSubmit(id, product_name){  
  2.   
  3. var data = {  
  4. id,  
  5. product_name  
  6. };  
  7.   
  8. this.items.push(data);  
  9. localStorage.setItem(this.storageKey, JSON.stringify(this.items));  
  10. var retrieverObject = localStorage.getItem('this.items.id');  
  11. var retrieveObject = JSON.parse(retrieverObject);  
  12. if (retrieveObject == id) {  
  13. alert('test');  
  14. }  
  15. }  
I want to check if the value has already been submitted. This is part of a shopping cart application in which the product quantity will be updated if the product has already been chosen.
When I use
  1. var retrieverObject = localStorage.getItem('MyDataStorageKey');  
it returns:
  1. [{"id":0,"product_name":"Adult Male Bike"},{"id":0,"product_name":"Adult Male Bike"}]  
All of the following return null
  1. var retrieverObject = localStorage.getItem('MyDataStorageKey.findIndex(id)');  
  2. var retrieverObject = localStorage.getItem('MyDataStorageKey.indexOf("id")');  
  3. var retrieverObject = localStorage.getItem('MyDataStorageKey.indexOf(id)');  
  4. var retrieverObject = localStorage.getItem('MyDataStorageKey[0].id');  
  5. var retrieverObject = localStorage.getItem('MyDataStorageKey[0]');  
  6. var retrieverObject = localStorage.getItem('MyDataStorageKey.findIndex("id")');  
  7. var retrieverObject = localStorage.getItem('MyDataStorageKey["id"]');  
  8. var retrieverObject = localStorage.getItem('MyDataStorageKey.id');  

Answers (4)