Нужен образец литерала объекта JavaScript - PullRequest
0 голосов
/ 19 января 2011

Мне нужен пример литерала объекта js, к которому можно получить доступ следующим образом:

hist.undo[0].operation[0].x // would print '2' or whatever
hist.undo[0].operation[0].y // would print '21' or whatever
// [...]
hist.undo[2].operation[0].x // would print '32' or whatever
hist.undo[2].operation[0].y // would print '12' or whatever

Спасибо! * * 1004

Ответы [ 3 ]

1 голос
/ 19 января 2011

( пример работы jsfiddle здесь )

var sample_operation_member = {"x": 100, "y": 250};
var sample_undo_member = { "operation" : [sample_operation_member, sample_operation_member,sample_operation_member] };

var hist = {
   "undo": [
      sample_undo_member,
      sample_undo_member,
      sample_undo_member,
      sample_undo_member
   ]
}

alert(hist.undo[0].operation[0].x);

Или, более подробно:

var hist = {
    undo: [
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]}
    ]
}

alert(hist.undo[0].operation[0].x);
0 голосов
/ 19 января 2011
hist = { "undo" : [
      {"operation":[
        {"x":2,"y":21},
        {"x":3,"y":31}
      ]},
      {"operation":[
        {"x":4,"y":41},
        {"x":5,"y":51}
      ]},
      {"operation":[
        {"x":6,"y":61},
        {"x":7,"y":71}
      ]}
     ]
    }
0 голосов
/ 19 января 2011
<html>
<head>
</head>
<body>
test
<script>
hist = {
 "undo" : [
  {
   "operation": [ {"x":"2","y":"21"}, {"x":"x2","y":"y21"} ]
  },
  {
   "operation": [ {"x":"99","y":"88"} ]
  },
  {
   "operation": [ {"x":"32","y":"12"} ]
  }
 ]
 };
alert(hist.undo[0].operation[0].x);
alert(hist.undo[0].operation[0].y);
alert(hist.undo[2].operation[0].x);
alert(hist.undo[2].operation[0].y);
</script>
</body>
</html>
...