Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i am using array.push method to add items to an array but every times it gives me last value and not all the values which i am getting from object,

here is my code.

JavaScript
function generatearray(obj)
{
 jsonObjEdt = []; 
 item = {};
for (j = 0; j < obj.length; j++) {
        $.each(obj[j], function (key, value) {
            var id = key;
            var Size = value;
            item[id] = Size;

        });
        jsonObjEdt.push(item);
        } 
}


my obj have 5 items and my jsonObjEdt also get 5 values but all five values are last from the obj. not sure why this happens ?
Posted

1 solution

Hi,

$.each itself is a loop why are you using for loop over it??

Just use $.each like this,

JavaScript
function generatearray(obj)
{
 jsonObjEdt = []; 
 $.each(obj, function (key, value) {
       jsonObjEdt.push(value); 
    });     
}


this will work for you..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900