代码之家  ›  专栏  ›  技术社区  ›  Rod

JSON对象的长度[重复]

  •  1
  • Rod  · 技术社区  · 16 年前

    var estoque={};
    function unpack_estoque(tnm,total,estoque_vl,id,tid,st) {
        tnm=tnm.split("|");
        total=total.split("|");
        estoque_vl=estoque_vl.split("|");
        id=typeof id != 'undefined' ? id.split("|") : null;
        tid=typeof tid != 'undefined' ? tid.split("|") : null;
        st=typeof st != 'undefined' ? st.split("|") : null;
    
        for (var i in tnm)
            estoque[i]={
                "id": id != null ? id[i] : null,
                "tid": tid != null ? tid[i] : null,
                "total": total[i],
                "estoque": estoque_vl[i],
                "tnm": tnm[i],
                "st": st != null ? st[i] : null
            };
    }
    

    estoque 在收集的项目中循环的长度?

    estoque.length 退货 undefined for (var i in estoque) 循环JSON对象,而不是estoque[]根数组。

    3 回复  |  直到 16 年前
        1
  •  2
  •   Yi Jiang G-Man    15 年前
    var estoque = {}; //object estoque.length will not work (length is an array function)
    var estoque = new Array(); //estoque.length will work
    

    你可以试试:

     var estoque = new Array();
     for (var i in tnm)
        estoque.push({
            "id": id != null ? id[i] : null,
            "tid": tid != null ? tid[i] : null,
            "total": total[i],
            "estoque": estoque_vl[i],
            "tnm": tnm[i],
            "st": st != null ? st[i] : null
        });
    

    现在estoque将是一个数组(可以作为一个数组遍历)。

        2
  •  1
  •   John    16 年前

    你不能让它工作,因为它是一个数组方法,而不是一个对象。您可以使用tnm的length属性作为度量。然后您可以使用:

    var tnmLength = tnm.length;
    for (i=0, i>tnmLength - 1; i++) {
         estoque[tnm[i]] = {};
    }
    

    循环遍历tnm数组中的所有项。

        3
  •  1
  •   Đọc truyện hay    11 年前
    a={a:{},b:{},c:{}}
    Object.keys(a).length
    3