www.webdeveloper.com
Results 1 to 4 of 4

Thread: Matching names from 1 array in another array

  1. #1
    Join Date
    Mar 2009
    Posts
    3

    Matching names from 1 array in another array

    Hi everyone,
    Just wondering if someone could give me a hand quickly. I have this piece of code that I want to be able to search for names in an array and if it finds them then it matches them.

    e.g. The code below would find 2 matches.

    Hope someone can help. Thanks

    Code:
    name = [Simon, Jim,Mark,Ryan];
    stored_names = [Jim,Simon,James,Simon,Tim];
    
    
    var match_checker = 0;   
    		
    	for (var count_index = 0; count_index < name.length; count_index = count_index ++) 
    	{	
    		if (name[count_index] == stored_names)
    		{
    			match_checker = match_checker + 1;    
    	}
    	document.write (match_checker);  //display numb of matches
    }	
    	}
    Last edited by timbrown2009; 03-27-2009 at 08:15 AM.

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    If you are using strings, quote them.

    var name = ['Simon','Jim','Mark','Ryan']

  3. #3
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    there are alot of things wrong with that mate tbh, cant really think right now, drink does that to ya but ill be on msn sometime tomorrow so gimme a shout then ok?

  4. #4
    Join Date
    Mar 2009
    Location
    Wherever i lay my hat
    Posts
    180
    Code:
    var i,
      j,
      count,
      names1=["Simon","Jim","Mark","Ryan"],
      names2=["Jim","Simon","James","Simon","Tim"],
      results="";
    for (i=0; i<names1.length; i+=1)
    {
      count=0;
      results+=names1[i]+" found ";
      for (j=1; j<names2.length; j+=1)
      {
        count+=names1[i]===names2[j]?1:0;
      }
      results+=count+" times\n"
    }
    alert(results);
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles