I have the following array with some items:
var arr = [
{id: 1, name: "Jon Snow"},
{id: 2, name: "Michael Scolfield"},
{id: 3, name: "Dexter Morgan"}
];
To add a new item, the push
method is used. Exactly this way:
arr.push({
id: 1,
name: "Jon Snow"
})
I noticed that it is possible to remove using the splice()
method by passing the index as a parameter. See:
arr.splice(index,1);
But I need to delete an item from array by passing the id
parameter, which would be an attribute of the object. Example:
function removeItem(id){
//remover o objecto com id específico
}
How to delete an item from an array by passing its id
parameter? What would be the best way to do this?