[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how does this work?

Posted on 2004-09-27
23
Medium Priority
?
220 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
Suggested Courses

834 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