1

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.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
Travis Michael Heller
  • 1,210
  • 3
  • 16
  • 34
  • 1
    The answer here http://stackoverflow.com/questions/5257786/whats-the-best-way-to-query-an-array-in-javascript-to-get-just-the-items-from-i looks like what you want. Namely `var wanted = items.filter( function(item){return (item.age==18);} );` – Skyl3r Aug 01 '16 at 16:46

1 Answers1

2

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"]]
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94