2

I am trying to run some JavaScript, but it is not working.

I have an object with two properties that are also objects.

var people = {
        me: {
            name: "Hello"
        },

        molly: {
            name: "Molly"
        }
    };

And I am trying to make a function that uses a for/in statement and an if statement to list the properties of people.

var search = function (x) {
        for (var a in people) {
            if (people.a.name === x) {
                return people.a;
            }
        }
    };

So the function loops through the properties of people and assigns them to the variable a. Therefore people.a will be equal to a property of people. Then the function returns the property (people.a).

So if I type in me as parameter x, will the function should return the properties for the me object? I put this code in jsLint and jsHint and it passed, but I decided to remove the corrections because they were useless.

I then want to print the object properties in the browser:

var print = search("me");
document.getElementById("p").innerHTML(print);

I have this linked to an html document, with a

tag id "p". I have tested javascript in the html document already, so I know that the javascript document is linked properly.

But the code will not work. Does anyone have any suggestions?

I have it working now thanks to the answers. But I thought that it would only print "Hello" to the screen, not { name: "Hello"}.

1
  • With your example, search("me") wouldn't return anything because there aren't any people with name: "me". Maybe you meant search("Hello"). Either way, there is another problem and the answers below describe that... Commented Aug 12, 2013 at 22:07

3 Answers 3

3

You need to use people[a], not people.a. The former looks for a property with the name of the value stored in a; the latter looks for a property literally named "a", which of course doesn't exist.

for (var a in people) {
    if (people[a].name === x) {
        return people[a];
    }
}

Fiddle here.

Also, I think you meant search("Hello"), right? If not, then it would just be var search = function(x) { return people[x]; }.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I did mean search("Hello"). So if the properties of an object are an object I need to use [ ] instead of . ? Also, if I added more properties to the me object would this code also print them onto the screen?
@user2620463 - if I understand your statement about "properties of an object are an object", that isn't correct. If you are trying to access the property of an object using the value of a variable, then you need to use bracket notation. In this case, when the variable a === "me", people[a] is equivalent to people["me"] is equivalent to people.me
1
people.a.name

you need to use the bracket operator if you want to access an item by name. Using people.a is literally searching for a member named 'a' instead of a member with the same name as the value of a.

Try:

people[a].name

instead.

Comments

0

4 errors in your code:

  • replace people.a with people[a]
  • replace innerHTML() with innerHTML
  • set HTML like this: document.getElementById("p").innerHTML = print.name;
  • As in a previous answer, search by name

Code: http://jsfiddle.net/nabil_kadimi/vVSPG/

2 Comments

Thank! This is exactly what I wanted to do. Do I only use [ ] if the property is another object?
You would use obj.a or obj['a'] if if the object has the property a that you are interested in.

Your Answer

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.