Push Array in Array Dynamically

Hello all,

I am trying to push an array in an array.
I’ve got the following so far:

var a = [];
for(i = 1; i < 3; i++){
	b = "john, alex"
	a.push([b].toString());
}
alert(a);

The outcome is [john, alex, john alex]

I need however the following outcome: [[john, alex],[john alex]]

I did something wrong in the rest of my code.
This code is working fine.
Sorry for this useless post.

So why are you converting the array to a string then?

I suspect that your code is incomplete, so I have modified it slightly to give you the end result I think you are trying to achieve.

<script type="text/javascript">
var a=[], b=[], build="", i=0;
  a = ["Bill", "Bob"];
  b = ["john", "alex"];
	a.push(b);
for(var i = 0; i < a.length; i++)
 { build+="i= "+i+" "+a[i]+" "+typeof a[i]+"\n";
 }
alert(build);
</script>

The output of this script is

i= 0 Bill string
i= 1 Bob string
i= 2 john,alex object

The object referred to for i=2 is the array b.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.