删除Java脚本中的子数组项 [英] Remove Sub Array Item in Javascript
本文介绍了删除Java脚本中的子数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从JavaScript/jQuery中id = 2
的以下数组中删除整个子数组项?
How would I remove the whole sub array item from the following array where id = 2
in JavaScript/jQuery?
arr = [{
"id": 1,
"name": "Steve"
},{
"id": 2,
"name": "Martin"
},{
"id": 3,
"name": "Short"
}]
我不确定在这种情况下如何使用grep
或splice
.
I am not sure how to use grep
or splice
in this situation.
我想要的输出将是:
newArr = [{
"id": 1,
"name": "Steve"
},{
"id": 3,
"name": "Short"
}]
推荐答案
Try to use Array.prototype.filter
at this context,
var arr = [ {"id":1,"name":"Steve"} , {"id":2,"name":"Martin"} , {"id":3,"name":"Short"} ];
var newArr = arr.filter(function(itm){
return itm.id !== 2;
});
这篇关于删除Java脚本中的子数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文