-5

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?

Sirko
  • 72,589
  • 19
  • 149
  • 183
user1470118
  • 412
  • 2
  • 9
  • 24

4 Answers4

2

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
}
jbabey
  • 45,965
  • 12
  • 71
  • 94
2

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

The Internet
  • 7,959
  • 10
  • 54
  • 89
  • Excellent! I was hoping it would work, but I wasn't sure. – user1470118 Jul 26 '12 at 13:17
  • So this answer is just a reiteration of the OP's code but with `Object` instead of `iScroll`? – nnnnnn Jul 26 '12 at 13:19
  • A good way to test out your script is by hitting Ctrl+Shift+J in Chrome and pasting the code into the console window. It's a handy JS REPL. At least that's what I did. – The Internet Jul 26 '12 at 13:19
  • @nnnnnn Yes, I'm assuming iScroll is an object, but without the implementation of iScroll I just used Object as a substitute. – The Internet Jul 26 '12 at 13:20
1

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

var arrayObjects = $.map(Array(numFields), function(){ return new iScroll(); } );
Engineer
  • 47,849
  • 12
  • 88
  • 91
  • 1
    And here I was thinking (facetiously) that there wasn't enough jQuery in the other answers. +1 though, I hadn't thought to use `.map()` for that sort of thing before. – nnnnnn Jul 26 '12 at 13:21
0

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 :)

Lemex
  • 3,772
  • 14
  • 53
  • 87
  • You could say `var myArray = [new iScroll(),new iScroll(),new iScroll(),new iScroll()]` – Esailija Jul 26 '12 at 13:12
  • Yes, good point, just wanted to keep it simple though i will update :) – Lemex Jul 26 '12 at 13:13
  • I don't think the OP needs it this simplified given that he or she came up with the correct syntax for a loop implementation without our help. (Especially when a loop is actually fewer lines of code as soon as you have three or more elements...) – nnnnnn Jul 26 '12 at 13:16