这是我的对象数组,名为 mycart [] :
[{" ID":" 6""量":" 20""价格&#34 ;: 1500," title":"休闲蓝条"}, {" ID":" 10""量":" 2""价格":1500,&# 34;标题":"休闲蓝 圆领"},{" id":" 5","数量":20,"价格":150,&# 34;标题":"休闲棕色 鞋"},{" ID":" 8""量":5,"价格":1500,&#34标题":"休闲蓝 条"},{" ID":" 19""量":7,"价格":1190,&#34标题":"儿童运动 穿"},{" ID":" 18""量":5,"价格":1290,&#34标题":"乐透运动"}]
现在,如果我想根据它的id删除完整的对象。例如:
如果我的身份证是10,那么我想删除
输出应该是:{" ID":" 10""量":" 2""价格":1500 ," title":"休闲的蓝色圆领"}
[{" ID":" 6""量":" 20""价格&#34 ;: 1500," title":"休闲蓝条"},{" id":" 5","数量" :20,"价格":150,"标题":"休闲棕色鞋"},{" id":" 8& #34;,"数量":5,"价格":1500,"标题":"休闲蓝条"},{&# 34; ID":" 19""量":7,"价格":1190,"标题":&#34 ;儿童运动服"},{" id":" 18","数量":5,"价格":1290, " title":" Lotto sports"}]
现在,我很惊讶只是匹配了object-id:
var p ='10';
for(var x=0; x < mycart.length; x++){
if(mycart[x].id == p){
found = true;
break;
}
答案 0 :(得分:1)
现在你需要添加:
mycart.splice(x, 1);
请参阅下面的代码段
var mycart = [{"id":"6","quantity":"20","price":1500,"title":"casual blue strip"},
{"id":"10","quantity":"2","price":1500,"title":"casual blue round neck"},
{"id":"5","quantity":20,"price":150,"title":"casual brown shoes"},
{"id":"8","quantity":5,"price":1500,"title":"casual blue strip"},
{"id":"19","quantity":7,"price":1190,"title":"kids sports wear"},
{"id":"18","quantity":5,"price":1290,"title":"Lotto sports"}];
var p ='10';
for(var x=0; x < mycart.length; x++){
if(mycart[x].id == p){
found = true;
break;
}
}
mycart.splice(x, 1);
document.write(JSON.stringify(mycart));