PDA

View Full Version : return index of array?



nikko50
01-26-2009, 09:09 AM
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??



// 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;




}

abduraooft
01-26-2009, 09:17 AM
You could use the function indexOf() (https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf)

oesxyl
01-26-2009, 09:17 AM
// 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

Philip M
01-26-2009, 09:21 AM
This?


// 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)

nikko50
01-26-2009, 09:45 AM
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??

abduraooft
01-26-2009, 09:51 AM
Add the following for IE

<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/arrayindexof-in-internet-explorer/
(A full featured code is provided in the mozilla website)

Philip M
01-26-2009, 10:11 AM
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/Core_JavaScript_1.5_Reference:Objects: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.

abduraooft
01-26-2009, 10:21 AM
By extending the Array.prototype, we can reuse the code in some other place, if required.

Philip M
01-26-2009, 11:07 AM
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.

mrhoo
01-26-2009, 05:06 PM
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.