本文介绍了查找阵列中的子阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有阵
array = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]];
和我想找到的子数组的索引
and I want to find the index of the sub array
[1, 2, 3, 4]
因此,它应该返回值为0结果
但每次我尝试这样做时,会返回-1
So it should return the value 0
but every time I try to do this, it returns -1
下面是我尝试:
array.indexOf(1, 2, 3, 4);
我知道这是因为子阵我正在寻找有逗号吧。结果
我怎样才能解决这个问题。
I know this is because the subarray i am searching for has commas in it.
how can i fix this
推荐答案
下面是一个快速和肮脏的解决方案,它的数组作为字符串比较:
Here is a quick and dirty solution which compares the arrays as strings:
var array = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]];
var target = [9, 10, 11, 12].toString();
for(var i=0; i<array.length; i++) {
if(target === array[i].toString()) break;
}
if(i >= array.length) {
console.log("not found");
} else {
console.log("found at index " + i);
}
这篇关于查找阵列中的子阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!