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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    102
    Thanks
    6
    Thanked 9 Times in 9 Posts

    How do I get the members of an arbitrary object?

    so basically, how do I write a function that does this.

    Code:
    function getMembers(obj)
    {
      var extractedMembers = new Array();
      for(member in obj)
      {
         extractedMembers.push(member);
      }
      return extractedMembers;
    }
    It extracts the members in any arbitrary object and returns them in an array.

    Many thanks!

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    three handy functions for raking objects:

    obVals : an array of the object's values (variants)
    obKeys : an array of the object's keys (strings)
    obMap : an array of the object's keys and values: [key,value] (arrays)



    Code:
    
    function obVals(ob) {var r = [];var i = 0;for (var z in ob) {if (ob && ob.hasOwnProperty && ob.hasOwnProperty(z)) {r[i++] = ob[z];}}return r;}
    function obKeys(ob) {var r = [];var i = 0;for (var z in ob) {if (ob.hasOwnProperty(z)) {r[i++] = z;}}return r;}
    function obMap(ob) {var r = [];var i = 0;for (var z in ob) {if (ob.hasOwnProperty(z)) {r[i++] = [z, ob[z]];}}return r;}
    
    
    
    //same code expanded
        function obVals(ob) {
            var r = [];
            var i = 0;
            for (var z in ob) {
                if (ob && ob.hasOwnProperty && ob.hasOwnProperty(z)) {
                    r[i++] = ob[z];
                }
            }
            return r;
        }
    
    
        function obKeys(ob) {
            var r = [];
            var i = 0;
            for (var z in ob) {
                if (ob.hasOwnProperty(z)) {
                    r[i++] = z;
                }
            }
            return r;
        }
    
    
        function obMap(ob) {
            var r = [];
            var i = 0;
            for (var z in ob) {
                if (ob.hasOwnProperty(z)) {
                    r[i++] = [z, ob[z]];
                }
            }
            return r;
        }
    
    
    //examples
    
    
    var ob = ({ a:1,b:2,c:3 });
    
    alert(obKeys(ob))// = a,b,c
    alert(obVals(ob))// = 1,2,3
    alert(obMap(ob).join("|"))// = a,1|b,2|c,3
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    hotwheelharry (10-12-2008)

  • #3
    Regular Coder
    Join Date
    Jun 2008
    Posts
    102
    Thanks
    6
    Thanked 9 Times in 9 Posts
    WOW!
    Thank you so much, that's exactly what I was looking for!


  •  

    Posting Permissions

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