Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    return index of array?

    I have the below array called "results". When I loop through all document elements I would like to check "field_name" against the "results" array and see if it exists and what index number it is at??


    Code:
    // Split the comma delimited response into an array 
        results = result.split("~"); 
    
       //Loop through array and populate fields 
    for(i=0; i<document.forms['theForm'].elements.length; i++) {   
    
    field_type = document.forms['theForm'].elements[i].type.toLowerCase();
    field_name = document.forms['theForm'].elements[i].name;
    
    
    
     
    }

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    You could use the function indexOf()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    nikko50 (01-26-2009)

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    // Split the comma delimited response into an array 
        results = result.split("~"); 
    
       //Loop through array and populate fields 
    for(i=0; i<document.forms['theForm'].elements.length; i++) {   
    
    field_type = document.forms['theForm'].elements[i].type.toLowerCase();
    field_name = document.forms['theForm'].elements[i].name;
    position = results.indexOf(field_name);
    if(position < -1){
      // don't exists
    }else{
      // exists and have index 'position'
    }
    
    }
    I don't know how this work in other browsers but will work in ffox.

    best regards

  • Users who have thanked oesxyl for this post:

    nikko50 (01-26-2009)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    This?

    Code:
    // Split the comma delimited response into an array 
        results = result.split("~"); 
    
     //Loop through array and populate fields 
    for(i=0; i<document.forms['theForm'].elements.length; i++) {   
    
    field_type = document.forms['theForm'].elements[i].type.toLowerCase();
    field_name = document.forms['theForm'].elements[i].name;
    
    for (var j = 0; j<= results.length; j++) {
    if (results[j] == field_name) {
    alert (field_name + " was found at results array index " + j);
    }
    }
    
    }

    Knowledge is of no value unless you put it into practice.”
    Anton Chekhov quotes (Russian playwright and master of the modern short story, 1860-1904)

  • The Following 2 Users Say Thank You to Philip M For This Useful Post:

    nikko50 (01-26-2009), oesxyl (01-26-2009)

  • #5
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    Thanks guys. Philips code works great but I would prefer to use the indexOf due to it may be faster if I have to loop through large sets. When I use indexOf I get an error method not supported. Does anyone know for sure if it's supported in IE??

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Add the following for IE
    Code:
    <script type="text/javascript">     
          if(!Array.indexOf){
    	    Array.prototype.indexOf = function(obj){
    	        for(var i=0; i<this.length; i++){
    	            if(this[i]==obj){
    	                return i;
    	            }
    	        }
    	        return -1;
    	    }
    	}
           /*test */
    	var array = [2, 5, 9];
    	index = array.indexOf(5);
    	alert(index);
        </script>
    See http://soledadpenades.com/2007/05/17...rnet-explorer/
    (A full featured code is provided in the mozilla website)
    Last edited by abduraooft; 01-26-2009 at 09:54 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Quote Originally Posted by nikko50 View Post
    Thanks guys. Philips code works great but I would prefer to use the indexOf due to it may be faster if I have to loop through large sets. When I use indexOf I get an error method not supported. Does anyone know for sure if it's supported in IE??
    See:- https://developer.mozilla.org/En/Cor...:Array:indexOf

    indexOf is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard.

    That's why I proposed the loop method. I don't think that speed of execution is of any significance in this day and age - we are talking about milliseconds at the most. And abduaraooft's work-around (above) seems to me to offer no advantages.

  • #8
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    By extending the Array.prototype, we can reuse the code in some other place, if required.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Quote Originally Posted by abduraooft View Post
    By extending the Array.prototype, we can reuse the code in some other place, if required.
    True, but is there really any great advantage in that? We are talking about a few lines of code only, and it is still a for loop. But chacun a son gout as the Italians would say.

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Abdurasoft's prototype.indexOf extension for IE is not quite the same as the one native to firefox and webkit (safari and chrome).

    They provide a second argument, which is an integer specifiying where to start the search.
    This comes in handy in removing redundent items, or searching for multiple identical items in an array.


  •  

    Posting Permissions

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