php json 添加元素,小程序:如何动态增加删除JSON对象数组(附代码)

本篇文章给大家带来的内容是关于小程序:如何动态增加删除JSON对象数组(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

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

112fe90bf150d5bc4f313f6c37288cfd.gif

直接上代码:添加

{{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过滤方法,删除对象数组中的第几个对象。

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

相关推荐:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值