小程式 - 陣列追加兼本地儲存

青呢喃發表於2018-08-27

小程式原陣列

award_result_goods:[]
複製程式碼

生成的資料

award_result_goods:[
   {
    awardname:1,
    award:'',
    time:'2018-8-27',
    awardimg:''
   },
   {
    awardname:1,
    award:'',
    time:'2018-8-27',
    awardimg:''
   }
]
複製程式碼

設定本地儲存

 that.setData({ 
    award_one:{
      awardname: name,
      award: name,
      time: time,
      awardimg:awardimg
    }
 })
 var newarr = [that.data.award_one]; //物件轉為陣列
  
 this.setData({
    award_result_goods: that.data.award_result_goods.concat(newarr)  //陣列連結
})
 wx.setStorageSync('award_result', JSON.stringify(that.data.award_result_goods)); //本地儲存
複製程式碼

獲取本地儲存

  if (wx.getStorageSync('award_result')){
      var award_result = JSON.parse(wx.getStorageSync('award_result'));
      this.setData({
        award_result_goods: award_result
      })
  }
複製程式碼

相關文章