简体   繁体   中英

JQuery Remove array from array of arrays

So I have an array of 2-element arrays, and I'm wondering if any of you know how to remove an array given the two elements of an array.

For example, the array would contain the following elements:

[1, 3]
[2, 5]
[1, 1]

Given the two numbers 2 and 5 in that order, is there any way to remove the second array?

Search the array for a match by some method. You could try each comparing each list in the larger list to the list given to you, or create a list from the numbers given to you and pass that. Then compare item by item for each list until you match each item-for-item every one.

If you find it use that index and look into the splice method from the javascript library. For example my_lisy.splice(2,2) argument 1 refers to the index of the list, and argument two refers how many items to remove.

You can try something like this:

var arr = [[1, 3], [2, 5], [1, 1]];
var result = [];

for(var i = 0, len = arr.length; i < len; i++) {
   // If the element is the one we don't wan't, skip it
   if(arr[i][0] == 2 && arr[i][1] == 5) {
       continue;
   }
   // Otherwise, add it to the result
   result.push(arr[i]);
}

console.log(result); //[[1, 3], [1, 1]]

You can add or extract any logic from the loop to suit your needs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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