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.
所以我有一个2元素数组的数组,我想知道你们是否知道如何在给定数组的两个元素的情况下删除数组。
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?
鉴于该顺序中的两个数字2和5,有没有办法删除第二个数组?
2 个解决方案
#1
0
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
您可以从循环中添加或提取任何逻辑以满足您的需要
#2
2
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.
如果您发现它使用该索引并从javascript库查看splice方法。例如,my_lisy.splice(2,2)参数1引用列表的索引,参数2引用要删除的项目数。
#1
0
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
您可以从循环中添加或提取任何逻辑以满足您的需要
#2
2
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.
如果您发现它使用该索引并从javascript库查看splice方法。例如,my_lisy.splice(2,2)参数1引用列表的索引,参数2引用要删除的项目数。