0

I have a array into which I push new values iteratively, at the end of each iteration I want the array formed to be inserted into new array. Ex: if at the end of first iteration array=[1,2,3,4], then bigarray=[[1,2,3,4]]. After 2nd iteration, if array=[5,6,7,8], then bigarray=[[1,2,3,4],[5,6,7,8]] and so on.

So basically bigarray is to contain a list of arrays out of which I can obtain arrays. ex: bigarray[0] should give me a array [1,2,3,4]

What I am doing now is array.push(some values) and at the end bigarray.push(array). Doing this merges all the values into a single array like this bigarray=[1,2,3,4,5,6,7,8] out of which I can separate first array and second array.

piece of code:

            var array=[];


            for(var i=0; i<range;i++){
                var arraychart=[];
                var jan= resp.ChartDataList[i].jan;
                arraychart.push(jan);
                var feb= resp.ChartDataList[i].feb;
                arraychart.push(feb);
                var mar= resp.ChartDataList[i].mar;
                arraychart.push(mar);
                var apr= resp.ChartDataList[i].apr;
                arraychart.push(apr);
                var may= resp.ChartDataList[i].may;
                arraychart.push(may);
                var jun= resp.ChartDataList[i].jun;
                arraychart.push(jun);
                //var jan= resp.ChartDataList[i].jan;
                array.push(arraychart);
                alert (arraychart);

            }
            alert(array);

How do I achieve the above desired result??

3
  • Can you post the code? Commented Jun 24, 2015 at 12:33
  • 2
    Please show some real code. I assume that you do something like array.push(1, 2, 3) while should be doing array.push([1, 2, 3]) Commented Jun 24, 2015 at 12:33
  • I am not pushing hard coded data. Ia m pushing a array variable iteratively into anotheer array variable. Kindly check the code I updated Commented Jun 25, 2015 at 7:13

2 Answers 2

1

You should call array.push in this way (array.push([1,2,3])) instead of (array.push(1,2,3))

var bigarray = [];

bigarray.push( [1,2,3,4] );
bigarray.push( [5,6,7,8] );

console.log(bigarray); // prints [[1,2,3,4],[5,6,7,8]]

EDIT

After seeing your code, you don't have problems in your code, just print the array using console.log instead of alert, because alert will call .toString() of the big array, and flatten it

Sign up to request clarification or add additional context in comments.

5 Comments

I am not pushing hard coded data. Ia m pushing a array variable iteratively into anotheer array variable. Kindly check the code I updated
Ya mshaaban..I tried using console.log and my data seems to appear in desired form. However it is shown now in the following form [Array[1], Array[1], Array[1], Array[1], Array[1]] I suppose the 1 indicates one array inside each of the 6 elements. But how can I now access this array index wise. If I use [0] on this aaray, it returns again Array[6] as it has 6 elements. But how to see the element of this Array[6].
In short, I mean how do we access elements of an array-element of an array.
If I stored the first element of [Array[1], Array[1], Array[1], Array[1], Array[1]] in an array X and then try to aceess its first element as X[0]. I am getting the same value of X and X[0]. Why is so?
Just access it like this: array[0][0], array[0][1], ...array[N-1][M-1]
0

U can try something like this

bigArrray[bigArray.length] = subArray

But...

bigArrray.push([1,2,3])

...works as well for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.