I have an object that looks like this:
var myObj = {
name:"Bacon",
fat:20,
carb:40
}
Is there a way to do a loop on the keys of the object, to then get its value?
I have an object that looks like this:
var myObj = {
name:"Bacon",
fat:20,
carb:40
}
Is there a way to do a loop on the keys of the object, to then get its value?
You can traverse through javascript following way:
for (var key in p) {
if (p.hasOwnProperty(key)) {
alert(key + " -> " + p[key]);
}
}
This should work.
for (var key in myObj) {
if (myObj.hasOwnProperty(key)) {
var obj = myObj[key];
for (var prop in obj) {
if(obj.hasOwnProperty(prop)){
alert(prop + " = " + obj[prop]);
}
}
}
}