how to update local storage array items
You need write updated items to your local storage. For example if you have array stored by key 'myArray'
you can write something like this localStorageService.set ('myArray', angular.toJson (myNewArray))
. Where myNewArray
is your updated array.
please give me 1 Example
localstorage is stringbased so you have to convert your array or object to a string
window.localstorage.setItem('myArray', JSON.stringify([1, 2, 3, 4]));
var arrayItems = JSON.parse(window.localstorage.getItem('myArray'));
You should use angular way in ionic apps. So It’s highly recommended use angular-local-storage plugin.
- Install it via bower
bower install angular-local-storage -S
- Include it in our
index.html
like this<script src="lib/angular-local-storage/dist/angular-local-storage.min.js"></script>
- Include it to your module dependencies. Like this
angular.module ('starter', ['ionic', ... 'LocalStorageModule', ...]);
- In your
app.config
function set prefix of your app in local storage . Like this:localStorageServiceProvider.setPrefix ('MyMobileApp');
- Then you can use
localStorageService
to get,set and remove items from it. For example localStorageService.set (‘myArray’, angular.toJson ([1, 2, 3, 4])); will do the same aswindow.localstorage.setItem('myArray', JSON.stringify([1, 2, 3, 4]));
from @bengtler example. - To get items you can write code like this:
angular.fromJson (localStorageService.get ('myArray'))
Cool. Just to add, this has nothing to do with IONIC. As @AlexUA mentioned, you should use Angular to do this stuff.
UpdateObject(newobj){
let updateItem = this.myarray.find(this.findIndexToUpdate, newobj.id);
let index = this.myarray.indexOf(updateItem);
this.myarray[index] = newobj;
}
findIndexToUpdate(newItem) {
return newItem.id === this;
}