如何从JSON数组中删除特定元素(例如:使用其ID)?
删除按钮将执行此任务,并传递其ID。
例如,如何删除id = 2的项目:
注意:id号2可以是10或20的数组索引。
“id”:2,“date”:“2018年3月27日”,“名称”:“名称2”,“类型”: “会议”
var id = 1;
var events = [{
"id": id++,
"date": "26 Mar 2018",
"name": "name 1",
"type": "task"
},
{
"id": id++,
"date": "27 Mar 2018",
"name": "name 2",
"type": "meeting"
},
{
"id": id++,
"date": "27 Mar 2018",
"name": "name 3",
"type": "meeting"
}
];
events.push({
"id": id++,
"date": "25 Mar 2018",
"name": "name 4",
"type": "meeting"
});
答案 0 :(得分:0)
你可以这样做
var events = [{
"id": 1,
"date": "26 Mar 2018",
"name": "name 1",
"type": "task"
},
{
"id": 10,
"date": "27 Mar 2018",
"name": "name 2",
"type": "meeting"
},
{
"id": 15,
"date": "27 Mar 2018",
"name": "name 3",
"type": "meeting"
}
];
let entry_to_delete=events.find(e => e.id === 10);
events.splice(events.indexOf(entry_to_delete),1);
console.log(events);