for(var propName in myobj)
{
if(myobj.hasOwnProperty(propName)){
var item = myobj[propName];
if(item instanceof Array)
{
item.forEach(function(arrItem, index){
//woo!
});
}
}
}
I thought it might be useful to create a convenience method on the Object.prototype
:
(function(){
function enumerateProperties(cbHandler, ownProperty){
ownProperty = typeof(ownProperty) == "boolean" ? ownProperty : true;
if(typeof(cbHandler) != "function"){
throw new Error("invalid callback");
}
for(var propName in this){
if((!ownProperty) || this.hasOwnProperty(propName)){
cbHandler(propName, this[propName]);
}
}
}
Object.prototype["enumerateProperties"] =
Object.prototype["enumerateProperties"] || enumerateProperties;
})();
so now you can:
var o = {a:1, b:[1, 2, 3], c:3};
o.enumerateProperties(function(propName,propValue){
console.log(propName);
if(propValue instanceof Array){
console.log(propValue.join(", "));
}
else{
console.log(propValue);
}
});