标签云

微信群

扫码加入我们

WeChat QR Code

I have an array of objects:var items = [{ id: 1, text: "test1" }, { id: 2, text: "test2" }, { id: 3, text: "test3"}];I have the following object:var itemToRemove = { id: 2, text: "test2" };I want to check by id if itemToRemove exists in the items array.And remove it:// pseudo codeitems.remove(itemToRemove);I went through javascript array methods but found nothing that will do the job. Thanks!


Possible duplicate of How to remove item from array by value?

2019年06月26日01分28秒

Possible duplicate of How do I remove a particular element from an array in JavaScript?

2019年06月26日01分28秒

+1, but this won't work in crappy (i.e., IE < 9) browsers. You'd have to use some kind of a shim to add the filter method to the Array object.

2019年06月26日01分28秒

Robusto if (![].filter) Array.prototype.filter = function (callback, that) { var index = 0, length = this.length, list = []; while (index < length) { var item = this[index]; if (callback.call(that, item, index++, this)) list.push(item); } return list; };

2019年06月26日01分28秒