How to replace existing key values with new one in ionic local storage

this.storage.get('current').then(token => {
 this.presentAlert('Existing Current data',JSON.stringify(token));
 this.storage.set('current',this.data);

I wanna replace the existing current values with new data. But storage.set is not replacing it.
Should i use storage.remove() ? to first remove the existing values and then add new by setting it again?
What is the best way?
Thank you.

Yes! The local storage accept add value if exist array, but not change the value…

Your need delete the value and create again!

:slight_smile:

I don’t think I agree with the previous post.

alpha = "";
beta = "";
gamma = "";

constructor(private storage: Storage) {}

ngOnInit(): void {
  this.storage.ready()
  .then(() => this.storage.set("key", "apple"))
  .then(() => this.storage.get("key").then(a => this.alpha = a))
  .then(() => this.storage.set("key", "banana"))
  .then(() => this.storage.get("key").then(b => this.beta = b))
  .then(() => this.storage.set("key", "cherry"))
  .then(() => this.storage.get("key").then(c => this.gamma = c));
}
<ion-list>
  <ion-item><ion-label [innerText]="alpha"></ion-label></ion-item>
  <ion-item><ion-label [innerText]="beta"></ion-label></ion-item>
  <ion-item><ion-label [innerText]="gamma"></ion-label></ion-item>
</ion-list>

I see “apple”, “banana”, and “cherry” as I would expect.

1 Like