coffeescript遍历json对象

来源:互联网 发布:2017淘宝双十一营业额 编辑:程序博客网 时间:2024/05/17 02:48

直接给代码:

headers = a:"this is a"  ,b:"this is b"  ,c:"this is c"exheaders = e : "this is e",c:"this is c"headers[key] = value for key,value of exheadersalert "key:#{key},value:#{value}" for key,value of headersfor i in headers  headers[i] = exheaders[i]

这个例子中,有两个JSON对象:headers,exheaders。遍历的方法为:

for key,value of ...

以上代码编译成javascript为:

var exheaders, headers, key, value;headers = {  a: "this is a",  b: "this is b",  c: "this is c"};exheaders = {  e: "this is e",  c: "this is c"};for (key in exheaders) {  value = exheaders[key];  headers[key] = value;}for (key in headers) {  value = headers[key];  alert("key:" + key + ",value:" + value);}
从中也可以看到javascript遍历json的方法。使用for(var i = 0; i < json对象.length;i++)的方法是行不通的,因为json对象没有length的属性

所以,coffeescript下,遍历json对象的方法不能写成:

for i in headers
  headers[i] = exheaders[i]

它会编译成:

for (_i = 0, _len = headers.length; _i < _len; _i++) {  i = headers[_i];  headers[i] = exheaders[i];}





0 0
原创粉丝点击