Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: How do I get the members of an arbitrary object?

1. ## 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!

• 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```

• ## Users who have thanked rnd me for this post:

hotwheelharry (10-12-2008)

• 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
•