Javascript JSON对象的长度

Javascript JSON对象的长度,javascript,json,Javascript,Json,此函数正在生成一个包含json对象的数组: 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=

此函数正在生成一个包含json对象的数组:

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(estoque中的var i)
通过JSON对象而不是estoque[]根数组循环

谢谢

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将是一个数组(您可以作为一个数组进行遍历)。

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

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

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

重复:忘记所有那些复杂的方法,使用这个:
Object.keys(estoque).length
Great-method-Derek!不过,请注意,此方法与IE8不兼容。
a={a:{},b:{},c:{}}
Object.keys(a).length
3