【发布时间】:2011-03-15 13:58:16
【问题描述】:
这个函数正在生成一个带有 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 (var i in estoque) 循环通过 JSON 对象而不是 estoque[] 根数组。
谢谢。
【问题讨论】:
-
忘记所有那些复杂的方法,使用这个:
Object.keys(estoque).length -
伟大的方法德里克!不过,请注意,此方法与 IE 8 不兼容。
标签: javascript json