0
var house = new Object(floors: "4", color:"red", windows:"lots", bathrooms:"3");
var result ="";
for (var i in house)
{
 result +="house." + i + " is " + house.i + ".<br />";
}
document.body.innerHTML += result;

I want to output house.floors is 4.<br />house.color is red.<br />and so on.

Zain Shaikh
6,0636 gold badges45 silver badges67 bronze badges
asked Dec 28, 2010 at 17:53

2 Answers 2

3

The Object constructor doesn't work like that. Use an object literal instead.

var house = { floors: "4", color:"red", windows:"lots", bathrooms:"3" }

Additionally house.i will reference the i property, not the property with the name that is stored in the string i, you want house[i].

answered Dec 28, 2010 at 17:55
Sign up to request clarification or add additional context in comments.

Comments

2

Curly brackets:

var house = {floors: "4", color:"red", windows:"lots", bathrooms:"3"};

There's rarely a need (in fact I can't think of a reason) to use an explicit Object constructor call; just use {} for a new, plain, empty Object instance, and [] for a new, plain, empty Array instance. For objects with initial properties, use the "name:value" syntax like you did (except in curly brackets).

answered Dec 28, 2010 at 17:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.