zoukankan      html  css  js  c++  java
  • 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 exheaders
    
    alert "key:#{key},value:#{value}" for key,value of headers
    
    for 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];
    }





  • 相关阅读:
    vbs下载文件
    变量名自动变化
    VBS获得随机数,截图函数
    VBS定时关闭的弹窗
    VBS操作剪切板
    手动关闭端口
    win7,xp通用的打开文件浏览对话框的方法
    QTP全选页面的复选框
    SVN的使用
    工作中用到的前端内容整理
  • 原文地址:https://www.cnblogs.com/leftfist/p/4257849.html
Copyright © 2011-2022 走看看