Remove an item from an array by value
No need for jQuery or any third party lib for this, now we can use the new ES5 filter :
let myArray = [{ id : 'a1', name : 'Rabbit'}, { id : 'a2', name : 'Cat'}];
myArray = myArray.filter(i => i.id !== 'a1');
I'm not sure how much of a hassle it is to refer to array items by index. The standard way to remove array items is with the splice method
for (var i = 0; i < items.length; i++)
if (items[i] === "animal") {
items.splice(i, 1);
break;
}
And of course you can generalize this into a helper function so you don't have to duplicate this everywhere.
EDIT
I just noticed this incorrect syntax:
var items = [id: "animal", type: "cat", cute: "yes"]
Did you want something like this:
var items = [ {id: "animal", type: "cat", cute: "yes"}, {id: "mouse", type: "rodent", cute: "no"}];
That would change the removal code to this:
for (var i = 0; i < items.length; i++)
if (items[i].id && items[i].id === "animal") {
items.splice(i, 1);
break;
}
You can either use splice
or run a delete yourself. Here's an example:
for (var i = 0; i < items.length; i ++) {
if (items[i] == "animal") {
items.splice(i, 1);
break;
}
}