我有这个数组:
var array = [{"a":"1","b":"2"},{"a":"3","b":"1"},{"a":"5","b":"4"}]
现在我想删除这条线,比如说,a=5。所以之后数组看起来像这样:
var array = [{"a":"1","b":"2"},{"a":"3","b":"1"}]
我如何以最简单和最快的方式做到这一点?
我有这个数组:
var array = [{"a":"1","b":"2"},{"a":"3","b":"1"},{"a":"5","b":"4"}]
现在我想删除这条线,比如说,a=5。所以之后数组看起来像这样:
var array = [{"a":"1","b":"2"},{"a":"3","b":"1"}]
我如何以最简单和最快的方式做到这一点?
您可以使用jQuery.map
which 允许您为要删除的元素返回 null 。
例如:
var array = [{"a":"1","b":"2"},{"a":"3","b":"1"},{"a":"5","b":"4"}]
var newArray = $.map(array, function(e){
return (e.a == "5") ? null : e;
});
// newArray contains [{"a":"1","b":"2"},{"a":"3","b":"1"}]
现场示例(观看控制台):http: //jsfiddle.net/2Yz7f/
Javascript(非 jQuery)方法:http: //jsfiddle.net/VYKBc/
也许这就是你的答案
array.splice(2,1);