www.webdeveloper.com
Results 1 to 4 of 4

Thread: another way to replace for(var key in o) ?

  1. #1
    Join Date
    Apr 2005
    Posts
    210

    Talking another way to replace for(var key in o) ?

    Is code of for(var key in o) only way to enumerate properties of object o?

    Is there second way for enumerating properties of object o?

    Thanks.

    .

  2. #2
    Join Date
    Feb 2011
    Posts
    231
    Hi,
    There is also for each (variable in object).
    It works the same way as "for .. in" , but instead of iterating over an objects property names, it iterates over the property values.
    Example:
    Code:
    <script type="text/javascript"><!--
    var obj = {apple:100, bool:false, astring:"www.coursesweb.net"};
    for each (item in obj) {
      document.write(item + '<br />');
    }
    --></script>
    Result:
    Code:
    78
    false
    www.coursesweb.net

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    var o={a:1,b:2,c:3};
    
    function dump(key, index){
      return key + "= " +o[key];
    }
    
    alert(
     Object.keys(o).map(dump).join("\n")
    );

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by MarPlo View Post
    There is also for each (variable in object)
    according to who?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles