如何从数组类型脚本中基于ID删除JSON对象

Roh*_*esh 1 javascript typescript

我有一个购物车数组,我正在将以下元素推入其中:

 cart = [];
 this.cart.push(item);
Run Code Online (Sandbox Code Playgroud)

我也想cart[]基于id 从此数组中删除元素,对象结构如下:

[
    {
        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
    }
]
Run Code Online (Sandbox Code Playgroud)

我正在执行pop删除添加到数组的对象的操作,但是不幸的是,这是删除最后插入的项目。我不要

this.cart.pop(); 
Run Code Online (Sandbox Code Playgroud)

我该如何在打字稿中做到这一点?

Vla*_*nut 5

您可以使用数组过滤器

    var idToDelete = 1;
    this.car = this.cart.filter(item=>item.id !=idToDelete );
Run Code Online (Sandbox Code Playgroud)