1

Given my JSON

countries: [
     {country: "canada",
      capital: "ottawa",
      territories: [
          yukon: {
               capital: "yellowknife",
               ...
          }
          ...
      ]
      ...
     }
 ]

How can I get a reference to the yukon object for instance?

I want to have something like

yukon = countries[0].territories[0]

and then make edits to yukon that will reflect in the original JSON. For example,

yukon.size = 1123300;
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • Your syntax is broken. Territories is an array and must contain elements, not property value pairs. –  Dec 24 '14 at 10:04

1 Answers1

-1

You are already half the way

var yukon = countries[0].territories[0].yukon;

yukon.size = 1123300;
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
Santhosh S
  • 782
  • 5
  • 17
  • Before posting an answer, you should check it by running it somewhere. This wouldn't have run, because the original data object is malformed--it can't decide if it wants `territories` to be an array or an object. How can you suggest to someone how to access a property inside an object when the object is not even formed correctly? –  Dec 24 '14 at 11:34