溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

小程序怎么動態(tài)增加刪除JSON對象數(shù)組

發(fā)布時間:2020-12-21 14:23:53 來源:億速云 閱讀:450 作者:小新 欄目:移動開發(fā)

小編給大家分享一下小程序怎么動態(tài)增加刪除JSON對象數(shù)組,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

先看效果,在制作小程序時,經(jīng)常遇到類似這種情況:

小程序怎么動態(tài)增加刪除JSON對象數(shù)組

直接上代碼:

<view class="add-btn" bindtap='addItems'>添加</view>

<view wx:for="{{itemLists}}" wx:key="index" class='list'>
  <input type='text' value='{{item.id}}'></input>
  <text>{{item.time}}</text>
  <text class='delete-btn' data-idx='{{index}}' bindtap='deleteIitems'>刪除</text>
</view>
.add-btn{
  background: chocolate;
  width: 200rpx;
  text-align: center;
  color: white;
  margin-bottom: 10px;
}
.list{
  display: flex;
  justify-content: space-around;
  border: 1px solid;
}
.delete-btn{
  background: red;
}
Page({

  data: {
    itemLists: [
      { id: 1, time: '00:00:00' },
      { id: 2, time: '00:00:00' },
      { id: 3, time: '00:00:00' }
    ]
  },
  addItems() {
    let list = this.data.itemLists
    list.push({ id: ~~(Math.random()*100), time: '00:00:00' })
    this.setData({
      itemLists: list
    })
  },
  deleteIitems(e) {
    let idx = e.currentTarget.dataset.idx
    let list = this.data.itemLists
    let filterRes = list.filter((ele,index) => {
      return index != idx
    })
    this.setData({
      itemLists: filterRes
    })
  }

})

總結(jié):

關(guān)鍵處是使用ES6中的filter過濾方法,刪除對象數(shù)組中的第幾個對象。

過濾更多的時候是用在過濾掉指定的內(nèi)容。

看完了這篇文章,相信你對小程序怎么動態(tài)增加刪除JSON對象數(shù)組有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI