简体   繁体   English

从内部数组使用jquery的第一项从数组中删除数组

[英]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'] . 假设我要删除包含['human', 'mammal']的数组。 How would I remove it using the first value 'human' in that array? 如何使用该数组中的第一个值'human'将其删除?

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. 显然,此方法将不再起作用。

jQuery is not even required for this task. 此任务甚至不需要jQuery。 Array.prototype.filter() will be able to solve this problem : Array.prototype.filter()将能够解决此问题

 var myArray = [["human", "mammal"], ["shark", "fish"]]; var exclude = "human"; var filtered = myArray.filter(function(item) { return item[0] !== exclude; }); console.log(filtered); // [["shark", "fish"]] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM