Javascript Vue.js购物车中已存在id时的购物车数量增量
您好,我正在创建自己的购物车,每当id已经在购物车中时,我都会遇到数量增量问题,所以我的问题是如何在购物车列表中筛选id?因此,id不会重复,而是在购物车中增加已过滤的idJavascript Vue.js购物车中已存在id时的购物车数量增量,javascript,vue.js,e-commerce,Javascript,Vue.js,E Commerce,您好,我正在创建自己的购物车,每当id已经在购物车中时,我都会遇到数量增量问题,所以我的问题是如何在购物车列表中筛选id?因此,id不会重复,而是在购物车中增加已过滤的id cartList: [], productList:[], cartadd: { id: 0, name: 0, price: 0, quantity: 0, image: '' } viewStorageCart () { i
cartList: [],
productList:[],
cartadd: {
id: 0,
name: 0,
price: 0,
quantity: 0,
image: ''
}
viewStorageCart () {
if(localStorage.getItem('cartStorageList')) {
this.cartList = JSON.parse(localStorage.getItem('cartStorageList'))
}
},
addStorageCart (product) {
this.cartadd.id = product.id;
this.cartadd.name = product.product_name;
this.cartadd.price = product.product_price;
this.cartadd.quantity = 1;
this.cartadd.image = product.product_image;
this.cartList.push(this.cartadd);
this.cartadd = {}
this.storeCart();
},
removeStorageCart (product) {
this.cartList.splice(product, 1);
this.storeCart();
},
storeCart() {
let parsed = JSON.stringify(this.cartList);
localStorage.setItem('cartLicartStorageListst', parsed)
this.viewStorageCart();
},
只需签入cartList数组并增加数量(如果存在)
addStorageCart (product) {
var findProduct = this.cartList.find(o => o.id === product.id)
if(findProduct){
findProduct.quantity +=1;
return;
}
this.cartadd.id = product.id;
this.cartadd.name = product.product_name;
this.cartadd.price = product.product_price;
this.cartadd.quantity = 1;
this.cartadd.image = product.product_image;
this.cartList.push(this.cartadd);
this.cartadd = {}
this.storeCart();
}
只需签入cartList数组并增加数量(如果存在)
addStorageCart (product) {
var findProduct = this.cartList.find(o => o.id === product.id)
if(findProduct){
findProduct.quantity +=1;
return;
}
this.cartadd.id = product.id;
this.cartadd.name = product.product_name;
this.cartadd.price = product.product_price;
this.cartadd.quantity = 1;
this.cartadd.image = product.product_image;
this.cartList.push(this.cartadd);
this.cartadd = {}
this.storeCart();
}
谢谢你,先生,你的回答正好解决了我的问题,非常感谢!我怎样才能减少一次删除,找到产品并减少数量,如果数量变为0,将其从数组中删除。谢谢您,先生,您的回答正好解决了我的问题,非常感谢!如何在删除时减少,找到产品并减少数量,如果数量变为0,则将其从数组中移除。