I have this function to push data into the array and save it in localstorage. My problem is that my function pushes to the array/saves to localstorage even though the same profileID exists. I want something that checks if the same profileId already exists remove the object and from localstorage/array then push to local storage/array. In short i do not want duplicates of the same profileID saved in localstorage or the array. I also want to keep the most new object instead of old and new saved in the array for the same profilID
PUSHData(){
var myobj = {
Button: this.isReadMore,
Class: this.sampleElem.className,
ProfiliD: this.id,
};
const saved = JSON.parse(localStorage.getItem('CollapseState'));
if(saved != null){
this.array.push(myobj);
localStorage.setItem('CollapseState',JSON.stringify(this.array));
}else{
this.array.push(myobj);
localStorage.setItem('CollapseState',JSON.stringify(this.array));
}
this is what localStorage looks like now
0: {Button: true, Class: "ShowHide", ProfiliD: "115279"}
1: [{Button: true, Class: "ShowHide", ProfiliD: "115279"},…]
0: {Button: true, Class: "ShowHide", ProfiliD: "115279"}
1: [{Button: true, Class: "ShowHide", ProfiliD: "115192"}]
I would really appreciatie any help