1、arr.splice(index,howmany)
- index:表示从指定的位置上(哪里)删除元素;
- howmany:表示应该删除多少个元素,赋值为0就表示不删除元素;
2、arr.findIndex() 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置;如果没有符合条件的元素返回 -1
示例:
.wxml文件
<view wx:for="{{productList}}" wx:for-item="item" wx:key="index" data-id="{{index}}" bindtap="doDel">
<view class="">{{item.aa}}</view>
<view class="">{{item.bb}}</view>
</view>
.js文件
page({
data: {
productList: []
},
doDel: function (e) {
const that = this
that.data.productList.splice(that.data.productList.findIndex( index => index === e.currentTarget.dataset.id), 1)
that.setData({
productList: that.data.productList
})
}
})