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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help writing For..in loops

    Hello all!

    I am new to JS and just started following an online course, but the site I'm using is crap in places because the authors who know the concepts inside-out rush through explanations assuming everyone will follow.

    I have a big problem understanding how to write a "for...in" loop, within a function. I have some prior programming experience so I'm not new to loops and am usually pretty fluent with "for" loops.
    My problem is that searching for info doesn't help because it always seems to yield the same one-line, simplified explanation and I still have no idea how to even write a for..in loop. I don't understand how it "works" and I was hoping someone would be so kind as to explain to it me like I was 5year old, a simple example in addition would be awesome x)
    Thanks a lot!!
    Best regards,
    K

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,466
    Thanks
    3
    Thanked 495 Times in 482 Posts
    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 type="text/javascript">
    /*<![CDATA[*/
    var i=0; // start the count at 0
     for (;i<4;i++){  // increment i(the count) while i is less than 4
      alert('i = '+i);   // do what you want here
     }
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I know about K.I.S.S but if I take the time to write a message, I would very much like it if repliers actually read through the message they think they're replying to.
    I needed help with the "for .. in" notation ("for (var J in Obj).."). I understand its a For loop, which I already mentioned I could do btw, but I didnt't get how it worked. It took me about 15 sites to finally find a simple concise explanation which said that "it would go through the properties of a given object", which suddenly makes sense.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Have a look at

    http://javascriptweblog.wordpress.co...-for-in-loops/

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,455
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by kizeryakuza View Post
    "it would go through the properties of a given object", which suddenly makes sense.
    properties and methods - skipping over any marked as non-enumarable.

    For example the length property of arrays is non-enumerable and so for..in skips over it. The Array.max() method a script in your page added is not set as non-enumerable and so the for..in would include that with the other array properties and methods processed.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    (for in) also goes through the prototype'd properties, not just the own properties of a given object...

    @felgall:
    Array.max would not show up unless you were iterating Array() itself.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,455
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by rnd me View Post
    (for in) also goes through the prototype'd properties, not just the own properties of a given object...

    @felgall:
    Array.max would not show up unless you were iterating Array() itself.
    I meant Array.prototype.max - which is how you'd normally define a new method to use with Array - which you would of course just refer to as whateverTheNameOfTheArrrayIs.max when using it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I think maybe he is looking for a practical example?

    If so...
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <div>
    <ul id="listing"></ul>
    </div>
    
    <script type="text/javascript">
    (
      function( )
      {
      
          var people = {
             "Adam" : { age: 35, gender: "M", married: "Yes" },
             "Betty" : { age: 39, gender: "F", married: "No", divorced: "Yes" },
             "Carol" : { age: 17, gender: "F", married: "No", inschool: "Yes" }
          }
    
          var outerlist = document.getElementById("listing");
          for ( var person in people )
          {
              var oli = document.createElement("li");
              oli.appendChild( document.createTextNode(person) );
              var iul = document.createElement("ul");
              var items = people[person];
              for ( var item in items )
              {
                  var ili = document.createElement("li");
                  ili.appendChild( document.createTextNode( item + "::" + items[item] ) );
                  iul.appendChild(ili);
              }
              oli.appendChild(iul);
              outerlist.appendChild(oli);
          }
      }
    )();
    </script>
    </body>
    </html>
    Kind of a silly example, but it does demonstrate both for...in as well as nested objects and for...in on them.

    Is this what you were after, kizeryakuz?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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