Answer the question
In order to leave comments, you need to log in
How to make an object counter in localStorage?
Hello everyone, in general, you need to write an array of objects in localStorage, how to make sure that every time the page is updated, a counter property is created in the object, instead of adding to the previous array.
So that when the page is updated, let's say three times it was not so in localStorage -
[{"id":1,"obj_id":234,"count":0},{"id":1,"obj_id":234,"count":0},{"id":1,"obj_id":234,"count":0}]
[{"id":1,"obj_id":234,"count":3}]
Answer the question
In order to leave comments, you need to log in
var keyName = 'my_counter'
var emptyCounter = [{"id":1,"obj_id":234,"count":0}]
var myObject = JSON.parse(localStorage.getItem(keyName)) || emptyCounter
myObject[0].count++
localStorage.setItem(keyName, JSON.stringify(myObject));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question