Reputation: 205
I set the data to a key called 'todo' in Local Storage.
The structure is like this.
key: todo
value : [{"text":"sdf","idx":1,"curTime":"Sun Dec 22 2019","finYN":""}]
when a button is clicked, I want to update to "Y" when finYN is empty or "N".
If this is my current local storage structure I can use this code but I don't know what to do with the current structure.
key: text / value : "sdf"
key: idx / value : 1
key: finYN / value : "Y"
localStorage.setItem("finYN", JSON.stringify ("Y"));
Upvotes: 1
Views: 151
Reputation: 570
Localstorage values are plain strings, so you cannot manipulate stringified objects. You will have to replace the whole value.
var todos = JSON.parse(localStorage.getItem('todo'));
if(!todos[0]['finYN']){
todo['finYN'] = 'Y';
localStorage.setItem('todo',JSON.stringify(todos))
}
Upvotes: 1
Reputation: 1494
If you save the values as a string:
localStorage.setItem('todo', JSON.stringify({"text":"sdf","idx":1,"curTime":"Sun Dec 22 2019","finYN":""}));
then you can get the value of that localStorage key and turn it back into JSON:
var json = JSON.parse(localStorage.getItem('todo'));
from here you can easily check the value by accessing json.finYN
and then changing the JSON and when you are done, you can store it as a string again.
Upvotes: 2