做一个购物车选择功能 当全选的时候 是 把整个购物车数据都重新setData更快 还是 逐条setData选中状态更快?
var shopCartList = [{
id: 0,
name: "123",
productList: [{
id: 0,
name: "123",
isChecked: false,
},
{
id: 0,
name: "123",
isChecked: false,
},
{
id: 0,
name: "123",
isChecked: false,
},
{
id: 0,
name: "123",
isChecked: false,
}
]
}, {
id: 0,
name: "123",
productList: [{
id: 0,
name: "123",
isChecked: false,
},
{
id: 0,
name: "123",
isChecked: false,
},
{
id: 0,
name: "123",
isChecked: false,
},
{
id: 0,
name: "123",
isChecked: false,
}
]
}]
//全部替换
shopCartList.forEach((value, index) => {
value.productList.forEach((val, ind) => {
val.isChecked = true
})
})
this.setData({
shopCartList: shopCartList
})
//逐条替换
shopCartList.forEach((value, index) => {
value.productList.forEach((val, ind) => {
this.setData({
["shopCartList[" + index + "].productList["+ ind +"].isChecked"]: true
})
})
})
这样最快
var data = {} shopCartList.forEach((value, index) => { value.productList.forEach((val, ind) => { data["shopCartList[" + index + "].productList["+ ind +"].isChecked"] = true }) }) this.setData(data)
全部替换
单选 采用逐条替换
const newShopCartList = this.data.shopCartList;
newShopCartList.forEach((value, index) => {
value.productList.forEach((val, ind) => {
newShopCartList[index].productList[ind].isChecked = true
})
})
this.setData({
shopCartList:newShopCartList
})