如果一个对象存储在离子存储的数组中,我正在尝试修改一个部分。当 a === '4' 时,我想将数据 b 更改为“111”
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)));
});
}
}
执行我的函数后,我得到了:
key : "array"
value : "{"a":"7","b":"8","c":"9"}"
我想获得:
key : "array"
value : "[{"a":"1","b":"2","c":"3"},{"a":"4","b":"111","c":"6"},{"a":"7","b":"8","c":"9"}]"