我有一个购物车数组,我正在推动这些元素:
cart = [];
this.cart.push(item);
我还想根据id从这个cart[]
数组中删除元素,对象结构如下:
[
{
id:1,
imageUrl: "http://lorempixel.com/100/100/people?1",
author: "Windward",
handle: "@windwardstudios",
body: "Looking for a better company reporting or docgen app?",
totalLikes: 0,
iLike: false
},
{
id:2,
imageUrl: "http://lorempixel.com/100/100/people?1",
author: "Windward",
handle: "@windwardstudios",
body: "Looking for a better company reporting or docgen app?",
totalLikes: 0,
iLike: false
}
]
我正在执行pop
操作以删除添加到数组中的对象,但不幸的是,这是删除最后插入的项目。我不想要的。
this.cart.pop();
我怎样才能在打字稿中做到这一点?
答案 0 :(得分:4)
您可以使用Array filter
var idToDelete = 1;
this.car = this.cart.filter(item=>item.id !=idToDelete );