小程序怎么动态增加删除JSON对象数组-创新互联

小编给大家分享一下小程序怎么动态增加删除JSON对象数组,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、成都网站设计、高邑网络推广、小程序设计、高邑网络营销、高邑企业策划、高邑品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;创新互联为所有大学生创业者提供高邑建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

先看效果,在制作小程序时,经常遇到类似这种情况:

小程序怎么动态增加删除JSON对象数组

直接上代码:

添加


  
  {{item.time}}
  删除
.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
    })
  }

})

总结:

关键处是使用ES6中的filter过滤方法,删除对象数组中的第几个对象。

过滤更多的时候是用在过滤掉指定的内容。

看完了这篇文章,相信你对小程序怎么动态增加删除JSON对象数组有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


分享文章:小程序怎么动态增加删除JSON对象数组-创新互联
网站路径:http://pwwzsj.com/article/dejgss.html