我使用此函数将数据推送到数组中,并将其保存在本地存储中.我的问题是,即使存在相同的profileID,我的函数也会推送到数组/保存到本地存储.我想要判断相同的profileID是否已经存在的东西,从本地存储/数组中删除对象,然后推送到本地存储/array.简而言之,我不希望在本地存储或数组中保存相同配置文件ID的重复项.我还希望为相同的配置文件ID将最新的对象保存在数组中,而不是旧的和新的
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));
}
这就是本地存储现在的样子
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"}]
如果能帮上忙,我将不胜感激