like this:
var arr = [
{ name: "robin", age: 19 },
{ name: "tom", age: 29 },
{ name: "test", age: 39 }
];
I want to remove array item like this(an array prototype method):
arr.remove("name", "test"); // remove by name
arr.remove("age", "29"); // remove by age
currently, I do it by this method(use jQuery):
Array.prototype.remove = function(name, value) {
array = this;
var rest = $.grep(this, function(item){
return (item[name] != value);
});
array.length = rest.length;
$.each(rest, function(n, obj) {
array[n] = obj;
});
};
but I think the solution has some performance issue,so any good idea?
I would hope jQuery’s oddly-named
grepwould be reasonably performant and use the built-infiltermethod of Array objects where available, so that bit is likely to be fine. The bit I’d change is the bit to copy the filtered items back into the original array: