我有一个 json 数组,如下所示:
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 个对象,但实际上它可以有更多。)
我的页面上有一个 ajax 操作,它将从数据库中删除这些对象之一代表的行。我不想使用包含许多行和数据库调用的整个 json 刷新我的页面,而是想永久删除由itemID
使用 javascript 从我的页面中的数组中获取。
有很多方法可以做到这一点。
我想到的:
-
过滤掉一个项目:
ad = ad.filter(item => item.itemId != 'DELETED ID');
-
查找项目的索引并将其删除
var deletedItem = ad.find(item => item.itemId == 'DELETED ID');
ad.splice(ad.indexOf(deletedItem), 1);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)