SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with JavaScript for loop...

    Hi,

    I have written a loop for the following "ducumentum" Array which is in employees var... but I am getting strange result, it is printing only the last one "Nagesh".

    Am I doing anything wrong here...? please help...

    <script>
    var employees = {"documentum":[
    {"firstName":"Ravi"},
    {"firstName":"Murali"},
    {"firstName":"Nagesh"}
    ]
    };

    function employeesList(){
    for(i=0; i<employees.documentum.length; i++){
    var list = employees.documentum[i].firstName;

    }
    alert(list);
    }
    window.onload = employeesList;
    </script>

    Thank you so much for help...

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script>
    var employees = {"documentum":[
    {"firstName":"Ravi"},
    {"firstName":"Murali"},
    {"firstName":"Nagesh"}
    ]
    };
    
    function employeesList(){
    for(var list=[],i=0; i<employees.documentum.length; i++){
     list[i]=employees.documentum[i].firstName;
    
    }
    alert(list);
    }
    window.onload = employeesList;
    </script>
    
    </body>
    
    </html>

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi vwphillips,

    Thank you so much... everything is working fine... Thanks a ton.

    RAghavender


Bookmarks

Posting Permissions

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