需要示例javascript对象文字

时间:2011-01-19 01:57:04

标签: javascript json object literals

我需要一个示例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

谢谢!

3 个答案:

答案 0 :(得分:1)

working jsfiddle example here

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);

答案 1 :(得分:0)

<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>

答案 2 :(得分:0)

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}
      ]}
     ]
    }