从数据库中删除后,按ID从JSON数组中删除对象 [英] remove object from JSON array by id after delete from DB
问题描述
我有一个看起来像json的数组:
I have a json array that looks like:
var ad =[{"itemID":"195","issue":"first","buttonText":"First","date":1481200571","link":"https://example.com/link"},{"itemID":"197","issue":"other","buttonText":"Something Else","date":1481200571","link":"https://example.com/linkother"},{"itemID":"215","issue":"main","buttonText":"Important","date":1481200571","link":"https://example.com/linkmain"}];
(上面的示例有3个对象,但实际上可以有更多对象.)
(The above example has 3 objects, but in reality it could have many more.)
我的页面上有一个ajax操作,该操作将从这些对象之一代表的db中删除该行.我不想使用包含许多行和db调用的整个json刷新我的页面,而是想使用javascript从页面数组中永久删除由itemID
表示的对象.
And I have an ajax action on my page that will delete the row from the db that one of these objects represents. Rather than refresh my page with the entire json which is many rows and a db call, I would like to just permanently remove the object that is represented by itemID
from the array in my page using javascript.
推荐答案
有很多方法可以做到这一点.
There are many ways of doing it.
我想到的那些
-
过滤出一个项目:
filtering out an item:
ad = ad.filter(item => item.itemId != 'DELETED ID');
查找项目的索引并将其删除
finding index of an item and removing it
var deletedItem = ad.find(item => item.itemId == 'DELETED ID');
ad.splice(ad.indexOf(deletedItem), 1);
这篇关于从数据库中删除后,按ID从JSON数组中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!