www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 12-10-2005, 09:53 AM
    schephais schephais is offline
    Registered User
     
    Join Date: Dec 2005
    Posts: 12
    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);
    I expected the above code to return the array keys for the myArray array. However, because I used the Object prototype to append a method to Object, the "for...in" loop will begin acting unpredictably. I could patch this up to catch for the 'myMethod' key, but this seems rather cowboy (what if I want to further extend the Object model?)

    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.
    Reply With Quote
      #2  
    Old 12-10-2005, 09:58 AM
    vwphillips vwphillips is offline
    Registered User
     
    Join Date: Jun 2004
    Location: Portsmouth UK
    Posts: 2,167
    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>
    but what are you trying to do?
    __________________
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org.uk
    remove any spaces between java & script
    Reply With Quote
      #3  
    Old 12-10-2005, 10:17 AM
    Charles's Avatar
    Charles Charles is offline
    JavaScript Banned
     
    Join Date: Nov 2002
    Location: Baltimore, Maryland
    Posts: 11,881
    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
    Reply With Quote
      #4  
    Old 12-10-2005, 10:30 AM
    schephais schephais is offline
    Registered User
     
    Join Date: Dec 2005
    Posts: 12
    Thanks! This works well for numeric arrays, but what about associative arrays?
    Reply With Quote
      #5  
    Old 12-10-2005, 10:36 AM
    schephais schephais is offline
    Registered User
     
    Join Date: Dec 2005
    Posts: 12
    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).
    Reply With Quote
      #6  
    Old 12-10-2005, 03:16 PM
    schephais schephais is offline
    Registered User
     
    Join Date: Dec 2005
    Posts: 12
    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);
    As you can see, this uses the 'propertyIsEnumerable' Object method to check the each property of the array object that meets the 'in' operator. I haven't tested this in JScript, but it works in JavaScript 1.5 and is supported by ECMAScript 3.

    Thanks for your help, everyone.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:06 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.