how do i change one line and resave the JSON in the local storage
trophies={
level1:false,
level2:false,
level3:false,
}
how do i change only level1 to true?? pls help
thanks for the reply but nothing like this seems to work I change the value like they show but it doesnt change
What version of Cocos2d-js are you using and can provide the code you are using to attempt this task, please.
i am using version 2.3.4 of cocos creator
OwnedTrophies = {
a1: false,
a2: false,
a3: false,
a4: false,
a5: false,
a6: false,
a7: false,
a8: false,
a9: false,
a10: false,
a11: false,
a12: false,
a13: false,
a14: false,
a15: false,
a16: false,
a17: false,
a18: false,
a19: false,
a20: false,
};
cc.sys.localStorage.setItem('trophies', JSON.stringify(OwnedTrophies));
let non = JSON.stringify(cc.sys.localStorage.getItem(‘trophies’))
JSON.parse(JSON.parse(non)).a1 = true;
cc.sys.localStorage.setItem(‘trophies’, JSON.stringify(non));
let non = cc.sys.localStorage.getItem(‘trophies’);
non = JSON.parse(non).a1 = true; // <- Try this
cc.sys.localStorage.setItem(‘trophies’, JSON.stringify(non));
JSON.parse() returns a new object, so you should override your variable to it.
Also, code will looks better like this:
let non = JSON.parse(cc.sys.localStorage.getItem(‘trophies’));
non.a1 = true;
cc.sys.localStorage.setItem(‘trophies’, JSON.stringify(non));
thanks for the reply but it doesnt change a1 i dont know why everywhere i looked says it should change but for some reason it doesnt…
You have mistake in your code… I tried code in console, and you can see the result:
obj.a1 was false, but I saved true property then.
cc.sys.localStorage.setItem('test', JSON.stringify({a1: false, a2: true}));
const obj = JSON.parse(cc.sys.localStorage.getItem('test'));
obj.a1 = true;
cc.sys.localStorage.setItem('test', JSON.stringify(obj));
const objNew = JSON.parse(cc.sys.localStorage.getItem('test'));
try this code.
OMG it works thank you so muchhhhhhhhhhhhhhhh you helped me a lot!!!