3
Contributors
4
Replies
6
Views
4 Years
Discussion Span
Last Post by Taywin
0

1)Are you talking about comparing 2 array elements, or 1 item with another array? If it is only 1 item with another array, you simply go through each element in the array and compare each item with the value you want to compare. If it is equal, do the iframe and break out of the loop. If you have gone through the whole loop and still not found, it does not exist in the array.

arr2[..., ..., ...]
for (var i=0; i<arr2.length; i++) {
  if (arr2[i]==comparingItem) {
    ... do something
    break  // get out of the loop
  }
}

If you are talking about 2 arrays, you need to just add another loop around it.

arr1[..., ..., ...]
arr2[..., ..., ...]
for (var i=0; i<arr1.length; i++) {
  for (var j=0; j<arr2.length; j++) {
    if (arr2[j]==arr1[i]) {
      ... do something
      break  // get out of this loop
    }
  }
}

2) Don't know... You need to read a lot and practice a lot. One problem with JavaScript is that the language is too flexible and has tons of pitfalls. It is even worse when you attempt to make your script cross-browser. JQuery helps you that, but to me it is too shallow to only know how to do work in JQuery because JQuery is not the language but a library.

0

thank you for your quick response

// all links existing in this document 
arr1= ["http://www.page.com/q.html","http://www.afaq.com/vb/showthread.php?t=77","http://www.daniweb.com/web-development/javascript-dhtml-ajax"] 

arr2 = [a.mp3,b.wav,c.rm,d.wma,"http://www.page.com/q.html",f.ram]       
arr3= ["aa.wav","bb.flv","cc.mp3","http://www.daniweb.com/web-development/javascript-dhtml-ajax"]

arr4=[.....,"http://www.afaq.com/vb/showthread.php?t=77",....]

if (x[i] == arr1[any element from 0 to arr1.length]) {

// do something

}

its possible to do this without new for-loop ?

2) Don't know... You need to read a lot and practice a lot. One problem with JavaScript is that the language is too flexible and has tons of pitfalls. It is even worse when you attempt to make your script cross-browser. JQuery helps you that, but to me it is too shallow to only know how to do work in JQuery because JQuery is not the language but a library.

thanks alot

Edited by green-script

0

http://jsfiddle.net/U2wp2/

for (var i=0; i<arr1.length; i++) {
    var index = arr2.indexOf(arr1[i]);
    if (index >= 0) {
        document.write(arr1[i] + " in arr2 at index " + index + "<br/>");
    } else {
        document.write(arr1[i] + " not in arr2<br/>");
    }
}
0

The indexOf() function does not work in IE version 8 or lower. I am not sure if MS actually implements the function in IE9 or higher...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.