javascript - 如何按 Angular 从本地存储中按日期键删除日期

标签 javascript angular date

我正在尝试使用 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/

相关文章:

java - 为什么我的 javascript 数组从 java 对象获取未定义的值?

javascript - 使用 JQuery 获取 primefaces 输入文本值

angular - 如何将焦点设置在具有绑定(bind)的元素上?

sql-server - 在数据导入期间,如何将平面文件中的 dd-mmm-yy 值格式化为smalldatetime?

javascript - 如何使今天的日期等于特定日期?

java - 将日历转换为日期 - 忽略时区

javascript - 如何自动随机化 46 个名称以在 Google 表格中创建 46 x 6 唯一行和列?

javascript - JS 在完成之前先经过 forEach 循环

angular - JEST 和 AGM 模块的意外 token 导出

c# - 将 Angular 应用程序分解为多个 Visual Studio 项目的方法