Wednesday 25 September 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...