Travis Michael Heller
Travis Michael Heller

Reputation: 1248

remove an array from an array using the inner arrays first item with jquery

I am trying to remove a specific array from an array using the first items name.

For example:

myArray = [["human", "mammal"], ["shark", "fish"]];

I need to use the first item in this array to remove the array containing it.

Let's say I want to remove the array containing ['human', 'mammal']. How would I remove it using the first value 'human' in that array?

Before I had a two dimensional array I was able to remove the specific item from the array using this code:

var removeItem = productArray.indexOf(valueLabel);
if (removeItem != -1) {
    productArray.splice(removeItem, 1);
}

Obviously this method will not work anymore.

Upvotes: 1

Views: 100

Answers (1)

TimoStaudinger
TimoStaudinger

Reputation: 42480

jQuery is not even required for this task. Array.prototype.filter() will be able to solve this problem:

var myArray = [["human", "mammal"], ["shark", "fish"]];
var exclude = "human";

var filtered = myArray.filter(function(item) {
  return item[0] !== exclude;
});

console.log(filtered); // [["shark", "fish"]]

Upvotes: 2

Related Questions