0

I have this two arrays.

url = ["http://www.linkedin.com/in/jannuelanarna", "http://www.linkedin.com/in/jannuela", undefined, undefined];

publicUrl = ["http://www.linkedin.com/in/jannuelanarna", "http://www.linkedin.com/pub/jay-r-bautista/64/b29/45b", undefined, "http://www.linkedin.com/pub/ronilo-canson/75/927/4a3", "http://www.linkedin.com/pub/siddharth-chaudhary/33/aa1/8", "http://www.linkedin.com/in/rojohnh", "http://www.linkedin.com/pub/lara-martinez/74/777/a3b", "http://www.linkedin.com/pub/alena-ortega/69/72a/415", "http://www.linkedin.com/in/nivlek1416", "http://www.linkedin.com/pub/emmar-reveriza/59/a91/132", "http://www.linkedin.com/in/samsanchezcb", "http://www.linkedin.com/pub/mitch-stevens/6b/375/3a0", "http://www.linkedin.com/pub/irish-jane-sumadic/29/339/910", "http://www.linkedin.com/pub/joel-sumadic/45/31b/ab3", "http://www.linkedin.com/pub/luna-cielo-yniesta/68/4b2/690"];

What will be the code so that I could search if a url exist in an array?

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • 2
    possible duplicate of [array.contains(obj) in JavaScript](http://stackoverflow.com/questions/237104/array-containsobj-in-javascript). See also http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array – bfavaretto Sep 09 '13 at 21:29
  • Sir i've seen the link. But it's too advanced for me I think? :/ – Arman Jon Villalobos Sep 09 '13 at 21:32
  • Isn't it `url.indexOf('http://www.linkedin.com/in/jannuelanarna')`? (Should return `0` I think) – Dave Chen Sep 09 '13 at 21:36
  • But what if there is a different page? Then there will be different set of urls, but the publicUrl is static because it depends on the logged user. – Arman Jon Villalobos Sep 09 '13 at 21:39
  • You have to use [`indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf). You can either check one array at a time, or [concatenate the arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) and check the resulting array. – bfavaretto Sep 09 '13 at 21:43

2 Answers2

2

New demo: (click here) Click "Run with JS in top, right corner.

function arraysHaveDuplicate(needle, arr1, arr2) {
  //will return first duplicate or false
  for (var i=0; i<arr1.length; ++i) {
    if (arr2.indexOf(needle) !== -1) { //found match, return matched value
       return arr1[i];
    }
  }
  return false; //no match
}

--OLD ANSWER-- New answer above!!!

Here's one way you could do it.

var value = "http://www.linkedin.com/pub/luna-cielo-yniesta/68/4b2/690";
if (url.indexOf(value) !== -1 || publicUrl.indexOf(value) !== -1) {
  alert('Found: '+value); 
}
else {
  alert('Not found: '+value); 
}

Further, you could make this into a more reusable function, like so:

function testArrays(needle, arrays) {
  for (var i=0; i<arrays.length; ++i) {
    if (arrays[i].indexOf(needle) !== -1) {
      return true;
    }
  }
  return false;
}

if (testArrays(value, [url, publicUrl])) {
  alert('Found: '+value);  
}
else {
  alert('Not found: '+value); 
}

See my demo (click here). You'll probably need to click "Run with JS" in the corner so that it will sound the alerts.

m59
  • 43,214
  • 14
  • 119
  • 136
  • What my plan really is : // Get the connections of the current user // Get the current urls from an href tag in the page // If there is a same value in urls and publicUrls then // Delete the href tag Something like that. But I just can't compare the two dynamically. – Arman Jon Villalobos Sep 09 '13 at 21:45
  • @ArmanJonVillalobos Are you saying you need to make sure a value is found in BOTH arrays, not just one or the other?? – m59 Sep 09 '13 at 21:55
  • But my problem is, there is no value, there will be only two arrays, one for the URL which is different every page. So how to work on it?. I can't seem to understand the code or where to change to suit my problem :/ – Arman Jon Villalobos Sep 09 '13 at 21:58
  • Yes sir, I need to compare if there is a value in two arrays, if there is a match, then i will modify the DOM (specifically the href attribute to none) – Arman Jon Villalobos Sep 09 '13 at 21:59
  • @ArmanJonVillalobos Lol, no one else can understand your problem :) – m59 Sep 09 '13 at 21:59
  • @ArmanJonVillalobos The best I can gather now is that you're asking if ANY two values from both arrays are the same, and not looking for any specific value? – m59 Sep 09 '13 at 22:00
  • Hehe. Maybe I describe it wrongly. On your question, YES, i need to compare if there are the same values in the array. As you can see, http://www.linkedin.com/in/jannuelanarna is on both sides. I need to put a flag or something that if there is a match on the two arrays then I will edit the anchor tag where there is this href link. – Arman Jon Villalobos Sep 09 '13 at 22:04
  • Thanks too sir! I appreciate all the details you've given me. – Arman Jon Villalobos Sep 09 '13 at 22:13
  • @ArmanJonVillalobos no problem. Just note that my answer is more reusable than the accepted answer. Either way is fine. I doubt you would need to reuse it anyway. – m59 Sep 09 '13 at 22:15
  • Uhm. indexOf is finding on http://, even though there is different links but because of http://, they are all found. How to work on this? – Arman Jon Villalobos Sep 09 '13 at 22:23
  • @ArmanJonVillalobos Again, you are not speaking clearly. `indexOf()` when used on an array looks for the exact value, not just a piece of it. It sounds like you're using `indexOf()` to search a string rather than an array. – m59 Sep 09 '13 at 22:55
  • That is what I am pointing out sir, I need to search if url[1] == publicUrl[1]. Not necessarily same index, but from the array of URL find if there is a match on the array of publicURL. I am not still clear sir? :( – Arman Jon Villalobos Sep 09 '13 at 22:57
  • @ArmanJonVillalobos That's exactly what my answer does... Did you not look at my example (top of my answer)? It tests a value that does exist in both and a value that doesn't. 1 is found, the other isn't. Just like you're asking. – m59 Sep 09 '13 at 22:59
  • @m59: What does arr2.indexOf() do with no arguments? – Flight Odyssey Sep 10 '13 at 00:00
  • @FlightOdyssey good call! I forgot to put in the "needle". Updated answer and demo. – m59 Sep 10 '13 at 00:39
2

Try this: (EDIT: Ignore undefined duplicates)

var found=false;
for(var i=0;i<url.length;i++)
{
    if(url!==undefined && publicUrl.indexOf(url[i])!=-1)
    {
        alert('Found: ' + url[i]);
        found=true;
    }
}
if(found)
{
    alert('Found');
}
else
{
    alert('Not found');
}

array.indexOf(value) returns the position of the value in the array, or -1 if the value is not in the array.

Flight Odyssey
  • 2,267
  • 18
  • 25