0

i have two array the first one is

lineChart = [{type : 'line' , data : arrdata }] ;

second one is

mina = [{type : 'area' , data : [[1326844575000,10] ,[1326845955000,10]], color : 'H33FF00'},
        {type : 'area' , data : [[1326846575000,10] ,[1326848065000,10]], color : 'H33FF00'},
        {type : 'area' , data : [[1326848390000,10] ,[1326849755000,10]], color : 'H33FF00'} ];

when i push them together like :

mychart.push(lineChart);
mychart.push(mina); 

console.log(JSON.stringify(mychart)) ;

this is what i get

[{"type":"line","data":[]},[{"type":"area","data":[[1326844575000,10],[1326845955000,10]],"color":"H33FF00"},{"type":"area","data":[[1326846575000,10],[1326848065000,10]],"color":"H33FF00"},{"type":"area","data":[[1326848390000,10],[1326849755000,10]],"color":"H33FF00"}]]

My question is: How to make this result array as one array like this?

[{"type":"line","data":[]},{"type":"area","data":[[1326844575000,10],[1326845955000,10]],"color":"H33FF00"},{"type":"area","data":[[1326846575000,10],[1326848065000,10]],"color":"H33FF00"},{"type":"area","data":[[1326848390000,10],[1326849755000,10]],"color":"H33FF00"}]
1
  • 2
    lineChart is not an array, it's an Object. Please learn the difference. Commented May 7, 2012 at 15:58

2 Answers 2

3

Just push the first object into the array.

mina.push(linechart);

Also, if you specifically want the linechart at the beginning use mina.unshift(linechart);

http://jsfiddle.net/E2WT8/

mina = [{type : 'area' , data : [[1326844575000,10] ,[1326845955000,10]], color : 'H33FF00'},
        {type : 'area' , data : [[1326846575000,10] ,[1326848065000,10]], color : 'H33FF00'},
        {type : 'area' , data : [[1326848390000,10] ,[1326849755000,10]], color : 'H33FF00'} ];

lineChart = {type : 'line' , data : [] } ;

mina.unshift(lineChart);

alert(JSON.stringify(mina)) ;
Sign up to request clarification or add additional context in comments.

3 Comments

@MinaGabriel here is a working jsfiddle can you explain what's not working? jsfiddle.net/E2WT8
can you put this in an answer cause this is the write one i wanted to give you credit
@MinaGabriel both answers are correct (because you wrote this is the write one). Here is an jsfiddle of mine: jsfiddle.net/4Zwkx , but it's true, that qw3n's answer was faster.
1

First: lineChart is no array, it's an object.

mina is an array.

To add lineChart you can use mina.push(lineChart);.

Another way is var mychart = mina.concat([lineChart]);

Another solution is too merge them:

function arrayMerge(array1, array2) {
    var i, j, newArray = []; for(i=0,j=array1.length;i<j;++i) {
        newArray.push(array1[i]);
    }
    for(i=0,j=array2.length;i<j;++i) {
        newArray.push(array2[i]);
    }

    return newArray;
}

var mergedArray = arrayMerge(mina, [lineChart]);

JSfiddle: http://jsfiddle.net/4Zwkx/

Comments

Your Answer

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