[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

how does this work?

var howManyAccordionChilds = 5;

DataProvider = new Array();  //why is this line here?

for (i=0; i < howManyAccordionChilds; i++)
{
     DataProvider[i] = new Array();  //how come i have to have this line and the above line for this to work?
     DataProvider[i].push({name: "Bob", gender: "Male"}, {name: "ted", gender: "wow"});
     trace (DataProvider[i]);
}
0
gmailrules
Asked:
gmailrules
  • 13
  • 9
1 Solution
 
ZylochCommented:
Hi gmailrules,

That line there initiates the array. It may not be necessary, but it is good practice to allocate space for the array beforehand, and it makes sure the following code with the array works.

Regards,
Zyloch
0
 
ZylochCommented:
I believe this would also work:

DataProvider = new Array;
or even
var DataProvider = new Array;     //However, Array has to be capitalized

Regards...
0
 
ZylochCommented:
Ah, just reread your question. You need the second DataProvider[i] = new Array(); because the first sets up DataProvider as an array. The second sets each new element of DataProvider as an array.

DataProvider
--------------------------
DataProvider[0]
----------------
Name = "bob"
Gender = "male"
----------------
DataProvider[1]
----------------
Name = "bob"
Gender = "male"
---------------
-------------------

That's basically it, so let's say you want "Bob", you can do

DataProvider[0]["name"], DataProvider[1]["name"], etc.

Regards...
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
blue-genieCommented:
i believe you're telling the dataprovider component where its receiving its data from, in this case, you're assigning a variable to an array.
0
 
gmailrulesAuthor Commented:
Ok so im putting a bunch of arrays in a single array?


//how come this doesn't work?

var howManyAccordionChilds = 5;
DataProvider[5] = new Array();

for (i=0; i < howManyAccordionChilds; i++)
{
     DataProvider[i].push({name: "Bob", gender: "Male"}, {name: "ted", gender: "wow"});
     trace (DataProvider[i]);
}

//or this?

var howManyAccordionChilds = 5;
DataProvider = new Array();
DataProvider[5] = new Array();
for (i=0; i < howManyAccordionChilds; i++)
{
     DataProvider[i].push({name: "Bob", gender: "Male"}, {name: "ted", gender: "wow"});
     trace (DataProvider[i]);
}


if I am putting arrays in a single array could i instead change, DataProvider = new Array();  to DataProvider = new Object();  would that be me just putting a bunch of arrays in an object?
0
 
ZylochCommented:
Hmm... some of the above don't work? How odd. I must admit I'm more of a Javascript programmer, and the above looked like Javascript to me, so... but I don't see anthing wrong with it.
0
 
ZylochCommented:
Besides he fact that your loop starts at 0 and goes to 4. Because you haven't set, let's say DataProvider[0] as an array, you can't use push(). That might cause the error.
0
 
gmailrulesAuthor Commented:
Both of them give undefined 5 times in a row.  That is why I don't really understand why the first code works and the two other examples dont.
0
 
ZylochCommented:
Yes, because of your loop. You go through DataProvider[0] through DataProvider[4]. However, if you haven't set them each as an array, you cannot use push(). That's what's causing the undefined. In the second one above, for instance, only DataProvider[5] is set as an array. I'll bet you if you replaced DataProvider[i].push with DataProvider[5].push in that above example, it should work.
0
 
gmailrulesAuthor Commented:
Ok you were right.  I seem to have the idea that when I put,
DataProvider[5] = new Array();
that it creates an array called DataProvider with 5 places to put info. 0 through 4
if that's true then how come I have to put,
DataProvider = new Array();
before it?  Or else I get a bunch of undefineds?


thanks for your patience :P
0
 
ZylochCommented:
No problem, I believe the way you wish to do it would be done like this:

DataProvider = new Array(new Array(),new Array(),new Array(),new Array(),new Array()); but I'm not sure

Once you put DataProvider = new Array();, DataProvider becomes an array, and the brackets ([i]) make sense. Otherwise, they don't.
0
 
gmailrulesAuthor Commented:
var howManyAccordionChilds = 5;

DataProvider = new Array();

for (i=0; i < howManyAccordionChilds; i++)
{
     DataProvider[i] = new Array();
     DataProvider[i].push({name: "Bob", gender: "Male"}, {name: "ted", gender: "wow"});
     trace (DataProvider[i]);
}

The only thing I reallllly don't understand at all is why I need this line at all for this to work?
DataProvider = new Array();

Isn't this line
DataProvider[i] = new Array();
creating the arrays I need?

I probably just don't have a good grasp on arrays.
0
 
ZylochCommented:
Yes, it is, I was just showing you an alternate way--there are a lot of those in programming
0
 
gmailrulesAuthor Commented:
I know there are alternate ways, that's not what I'm looking for.  I want to understand the code i wrote initially.  I dont understand why i have to create the same array twice for the code to work right.
0
 
ZylochCommented:
But you don't create the same array twice. the i is a variable set in the for loop. After every time it goes through the for loop, i adds one to itself, so first is

DataProvider[0] = new Array(); then the next time through it becomes DataProvider[1] = new Arra(); etc.
0
 
gmailrulesAuthor Commented:
I understand that.  What I mean is, what is this array for: DataProvider = new Array();
why do I have to make it?

why not just: DataProvider[i] = new Array(); without the line: DataProvider = new Array();
0
 
ZylochCommented:
Because then DataProvider will be uninitiated as an array. It will be treated as a variable, so you need to declare it as an array.
0
 
gmailrulesAuthor Commented:
ok but am I not declaring DataProvider as an array with this line? DataProvider[i] = new Array();
0
 
ZylochCommented:
No. You're declaring an ELEMENT of DataProvider as an array. Basically, you're declaring a multi-dimensional array. Just like how you could say DataProvider[i] = "hello", the above sets it to an array.
0
 
gmailrulesAuthor Commented:
Ok I think I get it now.  I am creating a multidimensional array here?

In this line I'm creating the array, DataProvider = new Array(); and this line, DataProvider[i] = new Array(); I'm creating an array and putting it into the other array i created before?
0
 
ZylochCommented:
That's right :)
0
 
gmailrulesAuthor Commented:
Ok, you anwsered cleared it up for me.  Thanks a lot!  Sorry that I didn't understand sooner. :D
0
 
ZylochCommented:
Sorry I didn't understand you question sooner :) Well, glad to help
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now