var a={"n":{"22492109":1,"462440041":1,"54189894":1,"85357830":1,"8857136":1,"912841324":1}};想知道n的大小,
用a.n.length说没定义  
 for(var colname in a.n)
{
 alert( a.n[colname] ); //为1...
 alert( colname );  //22492109....}
有什么办法可以直接取得a.n的大小,不用for 来算

解决方案 »

  1.   

    参见 for...in 语句,如:function ForInDemo(){
       // 创建某些变量。
       var a, key, s = "";
       // 初始化对象。
       a = {"a" : "Athens" , "b" : "Belgrade", "c" : "Cairo"}
       // 迭代属性。
       for (key in a)   {
          s += a[key] + "<BR>";
       }
       return(s);
    }
      

  2.   


    // 在火狐下可以用
    <script type="text/javascript">
    <!--
    var a={"n":{"22492109":1,"462440041":1,"54189894":1,"85357830":1,"8857136":1,"912841324":1}}; 
    alert(a.n.__count__)
    //-->
    </script>在IE下 就for in循环吧
      

  3.   

    lz 晕菜了吧!明明是自定义对象,根本不是 Array!
      

  4.   

    lz 无非是想得到属性计数,用原型扩展吧!L@_@K
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <title> new document </title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
     </head> <body>
      <script type="text/javascript">
      <!--
    Object.prototype.propertyCount = function() {
    var count = 0;
    for (var p in this)
    {
    if (!(this[p] instanceof Function))
    count++;
    }
    return count;
    };
    Object.prototype.methodCount = function() {
    var count = 0;
    for (var p in this)
    {
    if (this[p] instanceof Function)
    count++;
    }
    return count;
    };var a={"n":{"22492109":1,"462440041":1,"54189894":1,"85357830":1,"8857136":1,"912841324":1}};
    alert(a.propertyCount()); // 1: n
    alert(a.methodCount());  // 2: propertyCount; methodCount
    alert(a.n.propertyCount()); // 6
    alert(a.n.methodCount()); // 2: propertyCount; methodCount
      //-->
      </script>
     </body>
    </html>