需要示例 javascript 对象文字

发布于 2024-10-12 09:44:54 字数 320 浏览 3 评论 0原文

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

谢谢!

I need a sample js object literal which can be accessed in the following way:

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

居里长安 2024-10-19 09:44:54

这里是工作 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);

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

Or, more verbosely:

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);
仅此而已 2024-10-19 09:44:54
<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>
<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>
若有似无的小暗淡 2024-10-19 09:44:54
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}
      ]}
     ]
    }
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}
      ]}
     ]
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文