Wednesday, September 25, 2019

How do you print out all properties of an object and the values assigned to them in JavaScript?

Lets say you have an object you created in javaScript and you want to print out the property and the corresponding value, how would you do that? Here is an example below. Obviously in real life I wouldn't keep making objects, but bear with me here.



var houseA = {

county: "Falls County",
school: "Trinity West Elementary School",
square_feet: 2500,
specs: "3 bedroom and 2 bath"
};

for(var prop in houseA) {
document.write(prop);
}


//This would out put
county
school
square feet
specs


How would you make it output the values with the properties?



I know you can do it mannually by doing below.




document.write(houseA.county);
document.write(houseA.school);


so on and so fourth.

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...