www.webdeveloper.com
Results 1 to 4 of 4

Thread: how to find out a character in an array?

  1. #1
    Join Date
    Nov 2011
    Posts
    22

    how to find out a character in an array?

    i have an array elmlist=["0","0.1","1"]

    so i need to find out if there is any value which includes "."

    using array.contains and indexOf will return true only if the value itself completely match with the searchstring.

  2. #2
    Join Date
    Apr 2010
    Posts
    18
    PHP Code:
    var elmlist=["0","0.1","1"]

    function 
    findInArray(arrstr) {
        for(var 
    i=0;i<arr.length;i++) {
            if(
    arr[i].indexOf(str) != -1) {
                return 
    true;
                
    //Use the following if you want to return the value
                //return arr[i];
            
    }
        }
        return 
    false;
    }

    alert(findInArray(elmlist"."));
    alert(findInArray(elmlist"-")); 

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Or something like this:

    Code:
    <script type="text/javascript">
    
    	var elmlist = ['0', '0.1', '1'];
    
    	alert(
    
    		elmlist.join('').indexOf('.') > -1 ? 'FOUND' : 'NOT FOUND'
    
    	);
    
    </script>
    JavaScript: Learn | Validate | Compact | bionoid

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    Or simply the boolean /\./.test(elmlist.toString()) !
    Code:
    var elmlist=["0","0.1","1"]
    alert(/\./.test(elmlist.toString()))
    
    // To find the range 
    var elmstr=elmlist.toString(),range=-1;
    if (/\./.test(elmstr)) range=elmstr.replace(/\.[\d,\+\-]+/,'').split(',').length-1;
    alert(range);
    Last edited by 007Julien; 02-21-2012 at 08:59 AM.

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