1

I am trying to clear the fields in my object (collectionFilter) from local storage, using localStorage.removeItem('collectionFilter') other fields are romved except the date fields, (NOTE PLEASE I am new to javascript and angular)

I tried this but it didn't work for me

Trying to remove particular value from local storage key

below is the code

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;
  });

local storage data

agency: ""
endDate: "2019-10-03"
invoiceAccountCode: ""
paymentChannel: ""
paymentProviderReference: ""
prr: ""
receiptNumber: ""
revenueItem: ""
revenueSource: "6"
startDate: "2019-01-01"
tin: ""

Expected result

agency: ""
endDate: ""
invoiceAccountCode: ""
paymentChannel: ""
paymentProviderReference: ""
prr: ""
receiptNumber: ""
revenueItem: ""
revenueSource: ""
startDate: ""
tin: ""
Iyke
  • 47
  • 10

2 Answers2

1

Another Way You Can Also Do Like This Way

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));
}
0

Something like this would do:

const keys = ["endDate", "startDate", "revenueSource"];

keys.forEach(key => localStorage.setItem(key, ""));
mbojko
  • 13,503
  • 1
  • 16
  • 26