Solved

how does this work?

Posted on 2004-09-27
23
204 Views
Last Modified: 2010-04-03
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
Comment
Question by:gmailrules
  • 13
  • 9
23 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158145
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158146
I believe this would also work:

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

Regards...
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158155
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 12158199
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
 
LVL 1

Author Comment

by:gmailrules
ID: 12158209
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158218
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158222
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
 
LVL 1

Author Comment

by:gmailrules
ID: 12158245
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158264
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
 
LVL 1

Author Comment

by:gmailrules
ID: 12158307
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158321
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:gmailrules
ID: 12158352
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158362
Yes, it is, I was just showing you an alternate way--there are a lot of those in programming
0
 
LVL 1

Author Comment

by:gmailrules
ID: 12158376
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158388
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
 
LVL 1

Author Comment

by:gmailrules
ID: 12158397
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158403
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
 
LVL 1

Author Comment

by:gmailrules
ID: 12158432
ok but am I not declaring DataProvider as an array with this line? DataProvider[i] = new Array();
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
ID: 12158452
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
 
LVL 1

Author Comment

by:gmailrules
ID: 12158457
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158461
That's right :)
0
 
LVL 1

Author Comment

by:gmailrules
ID: 12158464
Ok, you anwsered cleared it up for me.  Thanks a lot!  Sorry that I didn't understand sooner. :D
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12158474
Sorry I didn't understand you question sooner :) Well, glad to help
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now