coffeescript遍历json对象

网友投稿 563 2022-09-05

coffeescript遍历json对象

coffeescript遍历json对象

直接给代码:

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:干货技巧|关于Redis的16个使用技巧
下一篇:网站升级HTTPS操作步骤,php程序员必备
相关文章

 发表评论

暂时没有评论,来抢沙发吧~