作为对象的离子存储更新值

时间:2018-11-13 14:07:27

标签: ionic-framework ionic2 ionic3

我已经在论坛中寻找了一段时间,但无法正常工作。

基本上我使用设置数据

this.storage.set('data', [
  {id:1, name: 'a'}, {id: 2, name:'b'}
])

如何在不覆盖数组的情况下编辑id:2的名称?我做了一些设置,但最终覆盖了整个对象。

我尝试了这里的一种解决方案,但最终覆盖了对象

    // Get the entire data
this.storage.get('data').then(valueStr => {

  let value = valueStr[1];

   // Modify just that property
   value.name = "hello";

   // Save the entire data again
   this.storage.set('pets', JSON.stringify(value));

   console.log(valueStr)
});

希望我能得到一些帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我明白了

this.storage.get('data').then(valueStr => {

  let value = valueStr;

   // Modify just that property
   value[1].name = "hahaha"; <--- need to specify index

   // Save the entire data again
   this.storage.set('pets', value);

   console.log(valueStr)
});
相关问题