【问题标题】:Ionic Storage update value which is object离子存储更新值是对象
【发布时间】:2018-11-13 22:17:56
【问题描述】:

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

基本上我使用设置我的数据

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)
});

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

【问题讨论】:

    标签: ionic-framework ionic2 ionic3


    【解决方案1】:

    我明白了

    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)
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多