Javascript:如何基于项属性值删除数组项(JSON对象)?
像这样:Javascript:如何基于项属性值删除数组项(JSON对象)?,javascript,jquery,Javascript,Jquery,像这样: var arr = [ { name: "robin", age: 19 }, { name: "tom", age: 29 }, { name: "test", age: 39 } ]; 我想删除如下数组项(数组原型方法): 目前,我使用这种方法(使用jQuery): 但是我认为这个解决方案有一些性能问题,所以有什么好主意吗 我希望jQuery奇怪的名字grep能够合理地
var arr = [
{ name: "robin", age: 19 },
{ name: "tom", age: 29 },
{ name: "test", age: 39 }
];
我想删除如下数组项(数组原型方法):
目前,我使用这种方法(使用jQuery):
但是我认为这个解决方案有一些性能问题,所以有什么好主意吗 我希望jQuery奇怪的名字
grep
能够合理地执行,并在可用的情况下使用数组对象的内置filter
方法,这样bit可能会很好。我要更改的位是将过滤项复制回原始数组的位:
Array.prototype.remove = function(name, value) {
var rest = $.grep(this, function(item){
return (item[name] !== value); // <- You may or may not want strict equality
});
this.length = 0;
this.push.apply(this, rest);
return this; // <- This seems like a jQuery-ish thing to do but is optional
};
Array.prototype.remove=函数(名称、值){
var rest=$.grep(此,函数(项){
return(item[name]!==value);//我希望jQuery奇怪的名字grep
能够合理地执行,并在可用的情况下使用数组对象的内置filter
方法,这样就可以了。我要更改的位是将过滤后的项复制回原始数组的位:
Array.prototype.remove = function(name, value) {
var rest = $.grep(this, function(item){
return (item[name] !== value); // <- You may or may not want strict equality
});
this.length = 0;
this.push.apply(this, rest);
return this; // <- This seems like a jQuery-ish thing to do but is optional
};
Array.prototype.remove=函数(名称、值){
var rest=$.grep(此,函数(项){
return(item[name]!==value);//这与JSON无关。如果答案解决了您的问题,请单击它旁边的复选标记。谢谢。这与JSON无关。如果答案解决了您的问题,请单击它旁边的复选标记。谢谢。
Array.prototype.remove = function(name, value) {
var rest = $.grep(this, function(item){
return (item[name] !== value); // <- You may or may not want strict equality
});
this.length = 0;
this.push.apply(this, rest);
return this; // <- This seems like a jQuery-ish thing to do but is optional
};