Referencing Javascript Object in another object -


this question has answer here:

what's best way reference javascript object exists inside of javascript object? instance, taking data below, how refer "home" of individual, reference object (places) in same dataset?

var data = {     "people": [{         "name": "jack",         "age": "8",         "home": data.places[0].country     }, {         "name": "john",         "age": "9",         "home": data.places[1].country     }],     "places": [{         "country": "holland"     }, {         "country": "germany"     }] } 

split declaration data.places exist when reference it.

var data = {     "places": [{         "country": "holland"     }, {         "country": "germany"     }] }  // data defined , has property "places"  data.people = [{     "name": "jack",     "age": "8",     "home": data.places[0].country // no more error  }, {     "name": "john",     "age": "9",     "home": data.places[1].country // no more error either }] 

in question, object referencing elements on before had been defined. resulted in typeerror: cannot read property 'places' of undefined. in solution, broke apart object definition when define data.people data.places piece array exists.

objects don't have declared @ once.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -