mih*_*jlo 3 javascript for-loop match multidimensional-array
我有两个多维数组:
首先是类似的东西 (['one','one','three'],['four','five',five'],['one','one','one'])
而第二个是这样的 (['one','one','nine'],['one','one','one'],['two','two'],['two','two','two']...)
现在,我想要的是找到第一个数组与第二个数组的匹配第一个索引,但两个数组中至少前两个索引的位置也必须匹配,例如:
first_array(['one','one','three'],['four','five',five'],['one','one','one'])
会匹配
second_array(['one','one','nine'],['one','one','one'],['two','two'] ['two','two','two "] ...)
输出将是例如.'警报(' 匹配".).
我试过了
for(i=0; i<1; i++){
if(first_array[0] == second_array) console.log('Match');
else console.log('No match');
}
Run Code Online (Sandbox Code Playgroud)
但我总是得到'不匹配',尽管有一场比赛.PS在'for'循环中,我的i是i <1,因为我只想比较first_array的第一个索引和完整的second_array.
提前致谢
var md1 = [['one','one','three'],['four','five','five'],['one','one','one']];
var md2 = [['one','one','nine'],['one','one','one'],['two','two'],['two','two','two']];
//Iterate through all elements in first array
for(var x = 0; x < md1.length; x++){
//Iterate through all elements in second array
for(var y = 0; y < md2.length; y++){
/*This causes us to compare all elements
in first array to each element in second array
Since md1[x] stays fixed while md2[y] iterates through second array.
We compare the first two indexes of each array in conditional
*/
if(md1[x][0] == md2[y][0] && md1[x][1] == md2[y][1]){
alert("match found");
alert("Array 1 element with index " + x + " matches Array 2 element with index " + y);
}
}
}
Run Code Online (Sandbox Code Playgroud)
工作示例 http://jsfiddle.net/2nxBb/1/
归档时间: |
|
查看次数: |
11548 次 |
最近记录: |