-2

Been searching for an hour or o and can't find what I'm looking for. Any help appreciated.

I have a JSON object coming from an ajax call that contains a couple of other objects with numerical keys.

Eg.

{"1" : { "Name" : "George", "DOB" : "11/1/88", "NickName" : "Porky" },
"2" : { "Name" : "Pete", "DOB" : "6/6/85", "NickName" : "Bozo" }}

I now want to access the properties in these object independently. For example:

for(var key in obj) {
    alert(obj. something here .Name);
}

But I can't for the life of me find the right syntax. I have tried...

obj.1.Name (I've seen this where the key of each object is a string but not a number)

and...

obj[1].Name

Can someone please put me out of my misery? How can I say, create an array of [Name1, Name2, etc...]?

Edi G.
  • 2,432
  • 7
  • 24
  • 33
Barbs
  • 1,115
  • 2
  • 16
  • 30
  • `obj[key].Name` because of your `for...in` statement, `key` is the "name" of each property. – Cerbrus May 19 '15 at 06:49
  • var obj = {"1" : { "Name" : "George", "DOB" : "11/1/88", "NickName" : "Porky" }, "2" : { "Name" : "Pete", "DOB" : "6/6/85", "NickName" : "Bozo" }}; alert(obj['1']['Name']); – ozil May 19 '15 at 06:54
  • 1
    Cerbrus. That question doesn't deal with an object within an object, simply an object with properties inside. Hence the new question. – Barbs May 19 '15 at 06:57

1 Answers1

2

You're looking for brackets notation:

for (var key in obj) {
    alert(obj[key].Name);
}

In JavaScript, you can access an object property using either dot notation and a literal property name (obj.foo), or brackets notation and a string* property name (obj["foo"]). In brackets notation, the string can be from any expression, including a variable lookup.

Live Example:

var obj = {
  "1": {
    "Name": "George",
    "DOB": "11/1/88",
    "NickName": "Porky"
  },
  "2": {
    "Name": "Pete",
    "DOB": "6/6/85",
    "NickName": "Bozo"
  }
};
for (var key in obj) {
  snippet.log(obj[key].Name);
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

* In ES6 brackets notation will also support Symbol property names.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Tried that and it failed but at least I know I'm using the right syntax and the error might be somewhere else. Thanks. – Barbs May 19 '15 at 06:51
  • 1
    @Barbs: Did you parse the JSON? Remember that JSON is text that describes an object tree. A lot of people *say* JSON when they don't really mean it, but if you're getting back a string, you'll want to run it through `JSON.parse` to get the object tree. – T.J. Crowder May 19 '15 at 06:53
  • Hi, yes I am parsing the JSON. I found the solution by using obj.LastName. I didn't need the first numerical key because I had already run that through the for loop. Thanks for all the down votes (not targeted at you TJ). It amazes me how this question can be marked as a duplicate less than a minute after it was posted. Surely there needs to be time to read BOTH questions before that happens. oh well. – Barbs May 22 '15 at 02:54