1

I have that object sample pages:

{
"page_1":
   {
     "stats":
       {
         "stat_1": 20,
         "stat_2": 40
       }
     "ahkam":
       {
         //Staff
       }
   }
"page_2":
   {
      //staff
   }
}

How can i get access to the value of stat_2? I used that expression but i don't know why it does not work:

Object.keys(pages["page_1"].stats)["stat_1"]
4

3 回答 3

2

This is a visualisation of the data:

enter image description here

Therefore, to get there, you'd use:

var stat = pages.page_1.stats.stat_2;

If "page_1" is dynamic, then you can use the bracket notation (also known as the subscript notation):

var key = 'page_' + x;
var stat = pages[key].stats.stat_2;

If "stat_2" is also dynamic, then again, you can use the same notation:

var pageKey = 'page_' + x;
var statKey = 'stat_' + n;
var stat = pages[pageKey].stats[statKey];

Or you could use this notation for the whole expression:

var stat = pages[pageKey]['stats'][statKey];

Basically, a.b.c is equivalent to a['b']['c'], where in this case 'b' and 'c' are string literals, but can be any expression.

于 2013-07-20T09:28:37.557 回答
0

as

obj = { 
       field1: {
                stat1: "string";
       }
}


obj.field1.stat1
于 2013-07-20T09:27:52.487 回答
0

is this not enough, Assign that object to var Page = {....}

  Page.page_1.stats.stat_1
于 2013-07-20T09:29:51.403 回答