Javascript 如何从数组类型脚本中删除基于id的JSON对象
我有一个cart数组,我将元素推入其中,如:Javascript 如何从数组类型脚本中删除基于id的JSON对象,javascript,typescript,Javascript,Typescript,我有一个cart数组,我将元素推入其中,如: cart = []; this.cart.push(item); 我还想根据id从这个cart[]数组中删除元素,对象结构如下所示: [ { id:1, imageUrl: "http://lorempixel.com/100/100/people?1", author: "Windward", handle: "@windwardstudios", bod
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();
如何在typescript中执行此操作?您可以使用
你可以用
这个问题中没有JSON。JSON是一组格式化字符串的规则,因此可以将其解析为对象。对象本身就是对象。
var-jsonString='{“foo”:“bar”}
对此表示抱歉..但我使用的是var-data=JSON.parse(JSON.stringify(item));要将其转换为JSON,这个问题中没有JSON。JSON是一组格式化字符串的规则,因此可以将其解析为对象。对象本身就是对象。var-jsonString='{“foo”:“bar”}
对此表示抱歉..但我使用的是var-data=JSON.parse(JSON.stringify(item));若要将其转换为JSONCan,请让我知道如何使用此选项删除元素1 by 1 as no,并立即删除它。是否可以让我知道如何使用此选项删除元素1 by 1 as no,并立即删除它。
var idToDelete = 1;
this.car = this.cart.filter(item=>item.id !=idToDelete );