|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Looping over Array
Appending methods to the Object or Array objects breaks "for...in" loop use:
Code:
Object.prototype.myMethod = function ()
{
return "my result";
}
var myArray = new Array('red','green','blue','pink');
for(myKey in myArray)
alert(myKey);
A Google search for solutions to this revealed the following quip from someone's blog: "...using the 'in' operator on arrays is incorrect." He didn't, however, offer any alternative. Does anyone know of a more reliable method of looping over arrays? Edit: Charles pointed out that in the above example, the array keys will all be numeric so I can use a regular for or while loop to loop over all the elements in the array. However, what about associative arrays? Also, am I making a mistake in my use of the prototype - is there another method of appending methods to objects without also appending them as properties? SOLVED I have now solved this problem. Please see my comment below ("SOLUTION...") for details. Many thanks. Last edited by schephais; 12-10-2005 at 03:18 PM. |
|
#2
|
|||
|
|||
|
well this bit works
Code:
<script language="JavaScript" type="text/javascript">
<!--
var myArray = new Array('red','green','blue','pink');
for(myKey in myArray){
alert(myKey);
}
//-->
</script>
__________________
Vic God loves you and will never love you less. http://www.vicsjavascripts.org.uk remove any spaces between java & script |
|
#3
|
||||
|
||||
|
Try:
Code:
var e, i = 0
while (e = array[i++]) {}
__________________
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.” —Tim Berners-Lee, W3C Director and inventor of the World Wide Web |
|
#4
|
|||
|
|||
|
Thanks! This works well for numeric arrays, but what about associative arrays?
|
|
#5
|
|||
|
|||
|
JavaScript allows programmers to append methods and properties to already initialized objects. In the above example, I've appended the (rather useless) function 'myMethod' to the Object object - the root object for all objects in JavaScript - with the intent of using it as a method.
Another of JavaScript's features is the ability to access object properties as array elements. So if 'myArray' is an array then myArray['myMethod'] returns the code used in the myMethod method. This is why the for...in loop doesn't work in the full example (when you incldude the myMethod declaration). |
|
#6
|
|||
|
|||
|
SOLUTION
I discovered the following solution to the above problem: Code:
Object.prototype.myMethod = function ()
{
return "my result";
}
var myArray = new Array('red','green','blue','pink');
for(myKey in myArray)
if(myArray.propertyIsEnumerable(myKey))
alert(myKey);
Thanks for your help, everyone. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|