hi

(1) e.x:
arr1 ["aa.wav","bb.flv","cc.mp3"];
arr2 ["http://www.page.com/q.html","http://www.afaq.com/vb/showthread.php?t=77","http://www.daniweb.com/web-development/javascript-dhtml-ajax"];

if x[i] == any element (arr2) >>> do <iframe......>

how to check if x[i] matches any element in arr2 ?

(2) what is the best way to learn "javascript, ajax, dhtml, jquery" in the net ?

thanks

Recommended Answers

All 4 Replies

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.

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

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/>");
    }
}

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.