我正在尝试使用 localStorage.removeItem('collectionFilter')
从本地存储中清除我的对象 (collectionFilter) 中的字段,除日期字段外,其他字段均被读取,(请注意,我是JavaScript 和 Angular 新手)
我尝试过这个,但它对我不起作用
Trying to remove particular value from local storage key
下面是代码
localStorage.removeItem('collectionFilter');
localStorage.setItem('collectionFilter', JSON.stringify(data));
this.collectionService.searchCollection(filter, event.itemsPerPage, offset)
.subscribe((payload) => {
if (this.page !== event.page) {
return;
}
this.queryResult = payload.queryResult;
this.totalAmount = payload.sum;
this.working = false;
});
本地存储数据
agency: ""
endDate: "2019-10-03"
invoiceAccountCode: ""
paymentChannel: ""
paymentProviderReference: ""
prr: ""
receiptNumber: ""
revenueItem: ""
revenueSource: "6"
startDate: "2019-01-01"
tin: ""
预期结果
agency: ""
endDate: ""
invoiceAccountCode: ""
paymentChannel: ""
paymentProviderReference: ""
prr: ""
receiptNumber: ""
revenueItem: ""
revenueSource: ""
startDate: ""
tin: ""
最佳答案
您也可以这样做的另一种方式
let savedCredentials = sessionStorage.getItem(credentialsKey) || localStorage.getItem(credentialsKey);
savedCredentials = JSON.parse(savedCredentials);
savedCredentials['property_ID'] = propertyId;
savedCredentials['name'] = propertyName;
savedCredentials['address'] = address;
savedCredentials['businessDate'] = businessDate;
const storage = this.isSessionStorageOrLocal();
if (storage === 'local') {
localStorage.removeItem(credentialsKey);
localStorage.setItem(credentialsKey, JSON.stringify(savedCredentials));
} else if (storage === 'session') {
sessionStorage.removeItem(credentialsKey);
sessionStorage.setItem(credentialsKey, JSON.stringify(savedCredentials));
}
关于javascript - 如何按 Angular 从本地存储中按日期键删除日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58200936/