html - Accessing nested objects in javascript -


i trying run javascript, not working.

i have object 2 properties objects.

var people = {         me: {             name: "hello"         },          molly: {             name: "molly"         }     }; 

and trying make function uses for/in statement , if statement list properties of people.

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

so function loops through properties of people , assigns them variable a. therefore people.a equal property of people. function returns property (people.a).

so if type in me parameter x, function should return properties me object? put code in jslint , jshint , passed, decided remove corrections because useless.

i want print object properties in browser:

var print = search("me"); document.getelementbyid("p").innerhtml(print); 

i have linked html document,

tag id "p". have tested javascript in html document already, know javascript document linked properly.

but code not work. have suggestions?

i have working answers. thought print "hello" screen, not { name: "hello"}.

you need use people[a], not people.a. former looks property name of value stored in a; latter looks property literally named "a", of course doesn't exist.

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

fiddle here.

also, think meant search("hello"), right? if not, var search = function(x) { return people[x]; }.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -