-1

Can anyone please help me. I am appending the data in local storage and it is taking duplicate values as well. Anyone know how to ignore duplicate values and append only unique values? I tried to check other similar types of stack-overflow, but not work for me, please help me! Please see the attached code and its output for reference:-

getProgramDetails = (ppTermDetails) => {
      this.programDetails = ppTermDetails;

      var entry = this.programDetails;  

      sessionStorage.setItem("entry", JSON.stringify(entry));

      var allEntries = JSON.parse(sessionStorage.getItem("allEntries")) || []; 

      allEntries.push(entry); 
      sessionStorage.setItem("allEntries", JSON.stringify(allEntries));  
     };

Please see the attached output for better understanding

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Dee
  • 69
  • 4
  • After reading `allEntries`, you would need to code some logic to check to see if `entry` already exists. If not, `.push` it, else do nothing. – Lil Devil Mar 19 '21 at 00:47
  • Does this answer your question? [How to remove all duplicates from an array of objects?](https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects) – Mohammed Mar 19 '21 at 00:50
  • @LilDevil That is the problem, I am not getting how to check my current value with the local storage values , is entry already exists or not ? How I can check and compare my current value with the local storage value ? If my current value already exists in the local storage, it avoid that duplicate value, but if the value is unique it will append in the local storage. – Dee Mar 19 '21 at 05:41

1 Answers1

0

Maybe a better way would be to use object and use programPerTermId as a key and so the new one would override the old one rather than having to loop through the array to find the duplicate one

assuming the programPerTermId is unique


{
  75: {programPerTermId: 75,....}
}

getProgramDetails = (ppTermDetails) => {

      sessionStorage.setItem("entry", JSON.stringify(ppTermDetails));

      var allEntries = JSON.parse(sessionStorage.getItem("allEntries")) || {}; 

      //we will use the programPerTermId as a key for the object
      //and will store the object as the value
      allEntries[ppTermDetails.programPerTermId] = ppTermDetails;

      sessionStorage.setItem("allEntries", JSON.stringify(allEntries));  
     };

but if you still want to go with your idea then you can check this thread and this, check the comments on the same topic

Mohammed
  • 133
  • 9
  • It's working in this way, but still I am not able to make it. Can you please tell me how can I apply if-else condition here or anything similar to that ? Actually, I am not getting how can I compare and check my current data is already exist in the local storage or not ? If similar, don't append the value, if unique append the value in local storage. Writing the code in the english, so that you get an idea. eg: If ( current value == already exists in the local storage) { console.log("already exists") } else { append the value in unique in the local storage } – Dee Mar 19 '21 at 05:35
  • I don't think there is a way in the local storage to do what you want. also you are not appending to the local storage you are replacing the whole data. when you do getItem you are getting the whole value so after parsing the JSON then you can do something like this `if(allEntries[ppTermDetails.programPerTermId])` but be careful that the object could be empty so maybe you should use Object.keys(...).length > 0. – Mohammed Mar 19 '21 at 15:17