如何从数组中删除某些数据
How to delete the certain data from array
我是ionic 4新手,正在做购物车功能。我想删除所选项目。但它删除像弹出功能不删除某些项目。我已经按照本教程进行操作:https://devdactic.com/dynamic-ionic-4-slides/
在服务中我正在使用这个功能
然后
Cart.page.ts
onDeleteItem(i) {
const index = this.selectedItems.indexOf(i);
if (index > -1) {
this.cartServ.deleteFromCart(i);
this.selectedItems.splice(index, 1);
console.log(this.selectedItems);
}
this.total = this.selectedItems.reduce((a, b) => a + (b.count * b.price), 0);
}
Cart.service
addToCart(product) {
this.cartService.addProduct(product);
}
deleteFromCart(i) {
const index = this.cart.indexOf(i);
if (index > -1) {
this.cart.splice(index, 1);
}
}
谁能帮帮我?
不要将 index
传递给 deleteFromCart
函数。
原因是 selectedItems
数组中删除项的 index
可能与 cart
项数组中的索引不同。
将项目 i
传递给 deleteFromCart
函数并找到 index
并使用 splice
.
onDeleteItem(i) {
...
this.cartServ.deleteFromCart(i);
...
}
deleteFromCart(i) {
const items = this.cart.filter(item => item.id === i.id);
const index = this.cart.indexOf(items[0])
if (index > -1) {
this.cart.splice(index, 1);
}
}
我是ionic 4新手,正在做购物车功能。我想删除所选项目。但它删除像弹出功能不删除某些项目。我已经按照本教程进行操作:https://devdactic.com/dynamic-ionic-4-slides/
在服务中我正在使用这个功能 然后 Cart.page.ts
onDeleteItem(i) {
const index = this.selectedItems.indexOf(i);
if (index > -1) {
this.cartServ.deleteFromCart(i);
this.selectedItems.splice(index, 1);
console.log(this.selectedItems);
}
this.total = this.selectedItems.reduce((a, b) => a + (b.count * b.price), 0);
}
Cart.service
addToCart(product) {
this.cartService.addProduct(product);
}
deleteFromCart(i) {
const index = this.cart.indexOf(i);
if (index > -1) {
this.cart.splice(index, 1);
}
}
谁能帮帮我?
不要将 index
传递给 deleteFromCart
函数。
原因是 selectedItems
数组中删除项的 index
可能与 cart
项数组中的索引不同。
将项目 i
传递给 deleteFromCart
函数并找到 index
并使用 splice
.
onDeleteItem(i) {
...
this.cartServ.deleteFromCart(i);
...
}
deleteFromCart(i) {
const items = this.cart.filter(item => item.id === i.id);
const index = this.cart.indexOf(items[0])
if (index > -1) {
this.cart.splice(index, 1);
}
}