SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking Values of Array

    Howdy everyone.

    I'm trying to make a script that will check an array to see if it contains a certain value. If it finds the value in the array, it will do something.

    For example:

    Code:
    var examdates = new Array("17-03-2003", "18-03-2003", "19-03-2003");
     
    if (examdates == "19-03-2003"){
    do something
    }
    Obviously that doesn't work though. Any ideas how to get it to work?

    Thanks!
    Peter

  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var i;
     
    for(i=0; i<examdates.length; i++) {
      if(examdates[i] == "19-03-2003")  {
    	//do something
      }
    }
    i can never remember if you need to do < or <= but looping through your array will work.

  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by makeda
    Code:
    var i;
     
    for(i=0; i<examdates.length; i++) {
      if(examdates[i] == "19-03-2003")  {
    	//do something
      }
    }
    i can never remember if you need to do < or <= but looping through your array will work.
    Since it's more than likely that you might want to do this again sometime, I suggest you turn it into a function - e.g. isInArray(). Then you can do:
    Code:
    if (isInArray(examdates,"19-03-2003"))
    {
      //stuff here
    }
    PS - it'll be "<="
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or make a method
    Code:
    Array.prototype.contains = function( val )
    {
     for ( var item, i = 0; ( item = this[i] ); i++ )
      if ( item == val )
       return true;
     return false;
    }
     
    var myArray = ['one','two','three'];
     
    alert( myArray.contains('one') );
    alert( myArray.contains('four') );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought you might do that - I just didn't want to complicate things for peternm22...
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I got it to work.


Bookmarks

Posting Permissions

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