Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by bfavaretto, acdcjunior, Evan Trimboli, Bergi, Wesley Wiser Sep 10 '13 at 1:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
possible duplicate of array.contains(obj) in JavaScript. See also stackoverflow.com/questions/143847/… –  bfavaretto Sep 9 '13 at 21:29
    
Sir i've seen the link. But it's too advanced for me I think? :/ –  Arman Jon Villalobos Sep 9 '13 at 21:32
    
Isn't it url.indexOf('http://www.linkedin.com/in/jannuelanarna')? (Should return 0 I think) –  Dave Chen Sep 9 '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 9 '13 at 21:39
    
You have to use indexOf. You can either check one array at a time, or concatenate the arrays and check the resulting array. –  bfavaretto Sep 9 '13 at 21:43

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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 9 '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 9 '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 9 '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 9 '13 at 21:59
    
@ArmanJonVillalobos Lol, no one else can understand your problem :) –  m59 Sep 9 '13 at 21:59

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.

share|improve this answer
    
Hey Thanks. I think i could go on from here =). This is crystal clear. THanks so much! –  Arman Jon Villalobos Sep 9 '13 at 22:11
    
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 9 '13 at 22:22
    
@Arman - Maybe your problem is the duplicate undefineds - try the new version –  Flight Odyssey Sep 9 '13 at 23:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.