i think you nested 2 loops for the purpose of comparing them or whatever, so you may have something like this:
-
var list1 = [1, 2, 3, 4, 5, 6];
-
var list2 = ['a', 'b', 'c', 3, 'd', 'e'];
-
-
for (var i in list1) {
-
for (var j in list2) {
-
if (list1[i] == list2[j]) {
-
alert('found ' + list1[i] + ' in both lists');
-
}
-
}
-
its a good idea to break the entire looping when the inner condition returns true ... so you may do the following:
-
var list1 = [1, 2, 3, 4, 5, 6];
-
var list2 = ['a', 'b', 'c', 3, 'd', 'e'];
-
-
outerloop: for (var i in list1) {
-
for (var j in list2) {
-
if (list1[i] == list2[j]) {
-
alert('found ' + list1[i] + ' in both lists');
-
break outerloop;
-
}
-
}
-
but have in mind ... that this is not the best way to compare ... until you need two loops you may have n*m steps to do. what about n+m? ;) so have a look at the following possibilty:
-
var list1 = [1, 2, 3, 4, 5, 6];
-
var list2 = ['a', 'b', 'c', 3, 'd', 'e'];
-
var lookup = {};
-
-
for (var j in list2) {
-
lookup[list2[j]] = list2[j];
-
}
-
-
for (var i in list1) {
-
if (typeof lookup[list1[i]] != 'undefined') {
-
alert('found ' + list1[i] + ' in both lists');
-
break;
-
}
-
}
-
hope this helps ... ;)