JavaScript的发现以及基于键值在数组中删除对象 [英] javascript find and remove object in array based on key value
问题描述
我一直在试图就如何找到几个办法我对象数组,其中ID = VAR,如果发现,从数组中删除的对象,并返回对象的新数组。
数据:
[
{ID:88,名:让我们去测试},
{ID:99,名:玩得开心男生女生},
{ID:108,名:你是真棒!}
]
我能够使用jquery $ grep来搜索阵列;
VAR ID = 88;VAR的结果= $ .grep(数据,功能(E){
返回e.id == ID;
});
但我怎么能删除整个对象时,ID = = 88,并返回这样的数据:
数据:
[
{ID:99,名:玩得开心男生女生},
{ID:108,名:你是真棒!}
]
谢谢,一如既往,任何帮助将AP preciated:)
我可以grep阵列的ID,但我怎么能删除整个对象其中id == 88
块引用>仅仅通过相反的predicate筛选:
VAR数据= $ .grep(数据,功能(E){
返回e.id = ID!;
});I have been trying several approach on how to find object i array, where ID = var, and if it found, remove the object from array and return the new array of objects.
Data:
[ {"id":"88","name":"Lets go testing"}, {"id":"99","name":"Have fun boys and girls"}, {"id":"108","name":"You are awesome!"} ]
I'm able to search the array using jquery $grep;
var id = 88; var result = $.grep(data, function(e){ return e.id == id; });
But how can I delete the entire object when id == 88, and return data like this:
Data:
[ {"id":"99","name":"Have fun boys and girls"}, {"id":"108","name":"You are awesome!"} ]
Thanks, as always, any help will be appreciated :)
解决方案I can grep the array for the id, but how can I delete the entire object where id == 88
Simply filter by the opposite predicate:
var data = $.grep(data, function(e){ return e.id != id; });
这篇关于JavaScript的发现以及基于键值在数组中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!