Giles Taylor Giles Taylor - 6 months ago 11
Javascript Question

Removing an object from an array using one value

Possibly a very obvious question from a beginner:

If I have the following array...

var arr =
{id: 1, item: "something", description: "something something"},
{id: 2, item: "something else", description: "something different"},
{id: 3, item: "something more", description: "more than something"}

... and wanted to delete a specific object within it by calling on the id (in this case by clicking on an div given the corresponding id)...

var thisItem = $(this).attr("id");

... could I do this without using a for loop to match
? And if so, how? I'm going to have a big array so running a for-loop seems very heavy handed.



You can use JQuery's grep

arr = jQuery.grep(arr, function(value) {
  return != id;