Question

Is it possible to create an array of objects? I'm wondering this because I'm developing an iPad website that has a bunch of fields that will need to be scrollable on the iPad.

Of course iPad/Safari had to go and make things difficult and not use the overflow-x/y option to allow scrollbars. So, what I'm hoping to do is create an array of iScroll objects because there's a large amount of things that need to be created and I would rather not do:

var myScroll = new iScroll();
var myScroll = new iScroll();

Etc.

What I would hope to do is have some sort of loop like:

var arrayOfObjects=new Array();
for(var i=0; i < numFields; i++)
{
   var temp = new iScroll();
   arrayOfObjects.push(temp);
}

So, is something like this doable?

Was it helpful?

Solution

I think the code you posted would work fine. A few nitpicks:

var arrayOfObjects = []; // use this syntax instead of "new array"
for (var i = 0; i < numFields; i++) {
   arrayOfObjects.push(new iScroll()); // no reason for a temp var
}

OTHER TIPS

Yes

var arrayOfObjects=new Array();
for(var i=0; i < 10; i++)
{
   var temp = new Object();
   arrayOfObjects.push(temp);
}

The above code produces: [Object, Object, Object, ... ] for the value of arrayOfObjects

What you want, can be achieved by jQuery.map() like this:

var arrayObjects = $.map(Array(numFields), function(){ return new iScroll(); } );

Yes as far as im away you can do this.

I would usually say:

var myArray = []
var myArray[0] = new iScroll();
var myArray[1] = new iScroll();
var myArray[2] = new iScroll();
var myArray[3] = new iScroll();

But doing it in a loop should work the same :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top