I'm trying to modify a part if an object stored in an array in Ionic Storage. I would like to change the data b to "111" when a === '4'
key : "array"
value : "[{"a":"1","b":"2","c":"3"},{"a":"4","b":"5","c":"6"},{"a":"7","b":"8","c":"9"}]"
modifyArray() {
// tslint:disable-next-line: forin
for (let i in this.array) {
this.storage.get('array').then(res => {
let y = JSON.parse(res);
let r = JSON.stringify(JSON.parse(res)[i]);
console.log('y : ' + JSON.stringify(y));
console.log('r : ' + r);
if (JSON.parse(r).a === '4') {
JSON.parse(r).b = '111';
}
this.storage.set('array', JSON.stringify(JSON.parse(r)));
});
}
}
After the execution of my function, I've got :
key : "array"
value : "{"a":"7","b":"8","c":"9"}"
I would like to obtain :
key : "array"
value : "[{"a":"1","b":"2","c":"3"},{"a":"4","b":"111","c":"6"},{"a":"7","b":"8","c":"9"}]"