views:

51

answers:

2

This function is generating a array with json objects on it:

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
        };
}

Now how do i get the estoque length to loop through the collected items?

estoque.length returns undefined while for (var i in estoque) loops through JSON object and not estoque[] root array.

Thanks.

+1  A: 

var estoque = {}; //object estoque.length will not work (length is an array function)

var estoque = new Array(); //estoque.length will work

You could try:

 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
    });

Now estoque will be an array (which you can traverse as one).

andreas
Thanks that did the job
Rodrigo
+1  A: 

You will not get that to work as it is an array method, not an object. You can use the length property of tnm as your measure. Then you can you can use:

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

To loop through all of the items in the tnm array.

John