Javascript: How To Remove An Array Item(json Object) Based On The Item Property Value?
like this: var arr = [ { name: 'robin', age: 19 }, { name: 'tom', age: 29 }, { name: 'test', age: 39 } ]; I want to remov
Solution 1:
I would hope jQuery's oddly-named grep
would be reasonably performant and use the built-in filter
method 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:
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
};
Post a Comment for "Javascript: How To Remove An Array Item(json Object) Based On The Item Property Value?"