tags:

views:

205

answers:

5

I have a bunch of things I want to add into an array, and I don't know what the size of the array will be beforehand. Can I do something similar to the c# arraylist in javascript, and do myArray.Add(object); repeatedly on it?

+10  A: 

just use array.push();

var array = [];


array.push();

This will add another item to it.

To take one off, use array.pop();

Link to JavaScript arrays: http://www.hunlock.com/blogs/Mastering%5FJavascript%5FArrays

Kevin
Sweet! I was looking in the wrong direction. Thanks!
SLC
+2  A: 

javascript uses dynamic arrays, no need to declare the size beforehand

you can push and shift to arrays as many times as you want, javascript will handle allocation and stuff for you

knittl
+2  A: 

Arrays are pretty flexible in JS, you can do:

var myArray = new Array();
myArray.push("string 1");
myArray.push("string 2");
JonoW
+2  A: 

With javascript all arrays are flexible. You can simply do something like the following:

var myArray = [];

myArray.push(object);
myArray.push(anotherObject);
// ...
JasonWyatt
A: 

You don't even need push-

var A=[10,20,30,40];

A[A.length]=50;

kennebec