Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to create a Multi dimensional array

Reply
Thread Tools

How to create a Multi dimensional array

 
 
SM
Guest
Posts: n/a
 
      04-27-2007
Hello
I'm trying to create a multi dimensional array in JavaScript, but
after some reading i still can't figure out how to apply it to my
model.

Here it is:
I have a list A and for each item in the list A i want to associate an
undetermined number of items. The complication for me is the fact that
the items to associate in list A are undetermined.

ie
LIST A
----------------
News
Entertainment
Politics

Items to associate
---------------------
News ---> Internacional, Asia, Europe,
Entertainment ---> Showbizz, Events
Politics ---> Local


i guess i can transalate it like this:

listA[0][0] = (News, Internacional);
listA[0][1] = (News, Asia) ;
listA[0][2] = (News, Europe);

listA[1][0] = (Entertainment, Showbizz);
listA[1][1] = (Entertainment, Events);

listA[2][1] = (Politics, Local);

I then would like to iterate through the array using a FOR statment

for(i=0; .... ; i++)
{
for(j=0; ...; j++)
{
...
}
}

How do i create this model using Javascript?

Thanks
Marco

 
Reply With Quote
 
 
 
 
-Lost
Guest
Posts: n/a
 
      04-27-2007
SM wrote:
> Hello
> I'm trying to create a multi dimensional array in JavaScript, but
> after some reading i still can't figure out how to apply it to my
> model.
>
> Here it is:
> I have a list A and for each item in the list A i want to associate an
> undetermined number of items. The complication for me is the fact that
> the items to associate in list A are undetermined.
>
> ie
> LIST A
> ----------------
> News
> Entertainment
> Politics
>
> Items to associate
> ---------------------
> News ---> Internacional, Asia, Europe,
> Entertainment ---> Showbizz, Events
> Politics ---> Local
>
>
> i guess i can transalate it like this:
>
> listA[0][0] = (News, Internacional);
> listA[0][1] = (News, Asia) ;
> listA[0][2] = (News, Europe);
>
> listA[1][0] = (Entertainment, Showbizz);
> listA[1][1] = (Entertainment, Events);
>
> listA[2][1] = (Politics, Local);
>
> I then would like to iterate through the array using a FOR statment
>
> for(i=0; .... ; i++)
> {
> for(j=0; ...; j++)
> {
> ...
> }
> }
>
> How do i create this model using Javascript?


listA[0] = ['News, Internacional', 'News, Asia', 'News, Europe'];
listA[1] = ['...', '...', '...'];

for (var i = 0; i < listA.length; i++)
for (var j = 0; j < listA[0].length; j++)
// listA[i][j]

You may want to use named arrays instead.

news = ['Internacional', 'Asia', 'Europe'];

Or better yet even still is to use an Object and JSON.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      04-27-2007
On Apr 26, 6:24 pm, SM <servandomont...@gmail.com> wrote:
> Hello
> I'm trying to create a multi dimensional array in JavaScript, but
> after some reading i still can't figure out how to apply it to my
> model.
>
> Here it is:
> I have a list A and for each item in the list A i want to associate an
> undetermined number of items. The complication for me is the fact that
> the items to associate in list A are undetermined.
>
> ie
> LIST A
> ----------------
> News
> Entertainment
> Politics
>
> Items to associate
> ---------------------
> News ---> Internacional, Asia, Europe,
> Entertainment ---> Showbizz, Events
> Politics ---> Local
>
> i guess i can transalate it like this:
>
> listA[0][0] = (News, Internacional);
> listA[0][1] = (News, Asia) ;
> listA[0][2] = (News, Europe);
>
> listA[1][0] = (Entertainment, Showbizz);
> listA[1][1] = (Entertainment, Events);
>
> listA[2][1] = (Politics, Local);


You could use an object with array properties. With an object literal
you could just print the data structure into the HTML page (assuming
we are talking about the web.)

var listA = {
news: ['internacional','asia','europe'],
entertainment: ['showbizz', 'events'],
politics: ['local']
};

You could dynamically add another category

listA.news.push('foo');

--------

You could use a function to build up the data structure

function add(list, cat1, cat2) {
if (!list[cat1]) {
list[cat1] = [];
}
list[cat1].push(cat2);
}

var listA = {};
add(listA, "News", "Internacional");
add(listA, "News", "Asia") ;
add(listA, "News", "Europe");
add(listA, "Entertainment", "Showbizz");
add(listA, "Entertainment", "Events");
add(listA, "Politics", "Local");

> I then would like to iterate through the array using a FOR statment
>
> for(i=0; .... ; i++)
> {
>
> for(j=0; ...; j++)
> {
> ...
> }
>
> }


for (var p in listA) { // loop through an object
for (var i =0; i< listA[p]; i++) { // loop through an array
alert(listA[p] + ": " + listA[p][i]);
}
}



> How do i create this model using Javascript?



You may want to read the first few chapters of a JavaScript language
book. This is the only recommended book.

<URL: http://jibbering.com/faq/#FAQ3_1>

Peter

 
Reply With Quote
 
SM
Guest
Posts: n/a
 
      04-27-2007
On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.com> wrote:
> On Apr 26, 6:24 pm, SM <servandomont...@gmail.com> wrote:
>
>
>
> > Hello
> > I'm trying to create a multi dimensional array in JavaScript, but
> > after some reading i still can't figure out how to apply it to my
> > model.

>
> > Here it is:
> > I have a list A and for each item in the list A i want to associate an
> > undetermined number of items. The complication for me is the fact that
> > the items to associate in list A are undetermined.

>
> > ie
> > LIST A
> > ----------------
> > News
> > Entertainment
> > Politics

>
> > Items to associate
> > ---------------------
> > News ---> Internacional, Asia, Europe,
> > Entertainment ---> Showbizz, Events
> > Politics ---> Local

>
> > i guess i can transalate it like this:

>
> > listA[0][0] = (News, Internacional);
> > listA[0][1] = (News, Asia) ;
> > listA[0][2] = (News, Europe);

>
> > listA[1][0] = (Entertainment, Showbizz);
> > listA[1][1] = (Entertainment, Events);

>
> > listA[2][1] = (Politics, Local);

>
> You could use an object with array properties. With an object literal
> you could just print the data structure into the HTML page (assuming
> we are talking about the web.)
>
> var listA = {
> news: ['internacional','asia','europe'],
> entertainment: ['showbizz', 'events'],
> politics: ['local']
>
> };
>
> You could dynamically add another category
>
> listA.news.push('foo');
>
> --------
>
> You could use a function to build up the data structure
>
> function add(list, cat1, cat2) {
> if (!list[cat1]) {
> list[cat1] = [];
> }
> list[cat1].push(cat2);
>
> }
>
> var listA = {};
> add(listA, "News", "Internacional");
> add(listA, "News", "Asia") ;
> add(listA, "News", "Europe");
> add(listA, "Entertainment", "Showbizz");
> add(listA, "Entertainment", "Events");
> add(listA, "Politics", "Local");
>
> > I then would like to iterate through the array using a FOR statment

>
> > for(i=0; .... ; i++)
> > {

>
> > for(j=0; ...; j++)
> > {
> > ...
> > }

>
> > }

>
> for (var p in listA) { // loop through an object
> for (var i =0; i< listA[p]; i++) { // loop through an array
> alert(listA[p] + ": " + listA[p][i]);
> }
>
> }
> > How do i create this model using Javascript?

>
> You may want to read the first few chapters of a JavaScript language
> book. This is the only recommended book.
>
> <URL:http://jibbering.com/faq/#FAQ3_1>
>
> Peter


Thank you guys for both of your answers.
The reason i want it to create an array is because after creating the
array i want to create 2 combobox using the JavaScript DOM and
populate the object options with the values of the array

The first combobox will be populated with the items in LIST A and the
second combobox will contain the items associated with selection in
the first combobox.

I already have the DOM function and now i have the array part.
For your info, the DOM goes something like this:

....
var tagForm = document.createElement('form');
var tagSelect = document.createElement('select');
tagSelect.setAttribute('name', 'Albums');


for (i=0; i<subalbumsElems.length; i++)
{
....
var tagOption = document.createElement('option');
tagOption.setAttribute('value', i);
tagOptionValue = document.createTextNode('....');
tagOption.appendChild(tagOptionValue)
tagSelect.appendChild(tagOption);

} //end for

tagForm.appendChild(tagSelect);
....


Thanks guys for all your help! Greatly appreciated
Marco

 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      04-27-2007
On Apr 26, 8:28 pm, SM <servandomont...@gmail.com> wrote:
> On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.com> wrote:
>
>
>
> > On Apr 26, 6:24 pm, SM <servandomont...@gmail.com> wrote:

>
> > > Hello
> > > I'm trying to create a multi dimensional array in JavaScript, but
> > > after some reading i still can't figure out how to apply it to my
> > > model.

>
> > > Here it is:
> > > I have a list A and for each item in the list A i want to associate an
> > > undetermined number of items. The complication for me is the fact that
> > > the items to associate in list A are undetermined.

>
> > > ie
> > > LIST A
> > > ----------------
> > > News
> > > Entertainment
> > > Politics

>
> > > Items to associate
> > > ---------------------
> > > News ---> Internacional, Asia, Europe,
> > > Entertainment ---> Showbizz, Events
> > > Politics ---> Local

>
> > > i guess i can transalate it like this:

>
> > > listA[0][0] = (News, Internacional);
> > > listA[0][1] = (News, Asia) ;
> > > listA[0][2] = (News, Europe);

>
> > > listA[1][0] = (Entertainment, Showbizz);
> > > listA[1][1] = (Entertainment, Events);

>
> > > listA[2][1] = (Politics, Local);

>
> > You could use an object with array properties. With an object literal
> > you could just print the data structure into the HTML page (assuming
> > we are talking about the web.)

>
> > var listA = {
> > news: ['internacional','asia','europe'],
> > entertainment: ['showbizz', 'events'],
> > politics: ['local']

>
> > };

>
> > You could dynamically add another category

>
> > listA.news.push('foo');

>
> > --------

>
> > You could use a function to build up the data structure

>
> > function add(list, cat1, cat2) {
> > if (!list[cat1]) {
> > list[cat1] = [];
> > }
> > list[cat1].push(cat2);

>
> > }

>
> > var listA = {};
> > add(listA, "News", "Internacional");
> > add(listA, "News", "Asia") ;
> > add(listA, "News", "Europe");
> > add(listA, "Entertainment", "Showbizz");
> > add(listA, "Entertainment", "Events");
> > add(listA, "Politics", "Local");

>
> > > I then would like to iterate through the array using a FOR statment

>
> > > for(i=0; .... ; i++)
> > > {

>
> > > for(j=0; ...; j++)
> > > {
> > > ...
> > > }

>
> > > }

>
> > for (var p in listA) { // loop through an object
> > for (var i =0; i< listA[p]; i++) { // loop through an array
> > alert(listA[p] + ": " + listA[p][i]);
> > }

>
> > }
> > > How do i create this model using Javascript?

>
> > You may want to read the first few chapters of a JavaScript language
> > book. This is the only recommended book.

>
> > <URL:http://jibbering.com/faq/#FAQ3_1>

>
> > Peter

>
> Thank you guys for both of your answers.
> The reason i want it to create an array is because after creating the
> array i want to create 2 combobox using the JavaScript DOM and
> populate the object options with the values of the array
>
> The first combobox will be populated with the items in LIST A and the
> second combobox will contain the items associated with selection in
> the first combobox.
>
> I already have the DOM function and now i have the array part.
> For your info, the DOM goes something like this:
>
> ...
> var tagForm = document.createElement('form');
> var tagSelect = document.createElement('select');
> tagSelect.setAttribute('name', 'Albums');
>
> for (i=0; i<subalbumsElems.length; i++)
> {
> ...
> var tagOption = document.createElement('option');
> tagOption.setAttribute('value', i);
> tagOptionValue = document.createTextNode('....');
> tagOption.appendChild(tagOptionValue)
> tagSelect.appendChild(tagOption);
>
> } //end for
>
> tagForm.appendChild(tagSelect);
> ...
>


Make sure you really cross browser test this. I remember something
strange when manipulating options in a select using dom functions. If
you have a problem (or maybe if you just want shorter code) you could
build HTML for the select with the options nested and then use
innerHTML to insert the select into the page.

Peter

 
Reply With Quote
 
SM
Guest
Posts: n/a
 
      04-27-2007
On Apr 26, 11:35 pm, Peter Michaux <petermich...@gmail.com> wrote:
> On Apr 26, 8:28 pm, SM <servandomont...@gmail.com> wrote:
>
>
>
> > On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.com> wrote:

>
> > > On Apr 26, 6:24 pm, SM <servandomont...@gmail.com> wrote:

>
> > > > Hello
> > > > I'm trying to create a multi dimensional array in JavaScript, but
> > > > after some reading i still can't figure out how to apply it to my
> > > > model.

>
> > > > Here it is:
> > > > I have a list A and for each item in the list A i want to associate an
> > > > undetermined number of items. The complication for me is the fact that
> > > > the items to associate in list A are undetermined.

>
> > > > ie
> > > > LIST A
> > > > ----------------
> > > > News
> > > > Entertainment
> > > > Politics

>
> > > > Items to associate
> > > > ---------------------
> > > > News ---> Internacional, Asia, Europe,
> > > > Entertainment ---> Showbizz, Events
> > > > Politics ---> Local

>
> > > > i guess i can transalate it like this:

>
> > > > listA[0][0] = (News, Internacional);
> > > > listA[0][1] = (News, Asia) ;
> > > > listA[0][2] = (News, Europe);

>
> > > > listA[1][0] = (Entertainment, Showbizz);
> > > > listA[1][1] = (Entertainment, Events);

>
> > > > listA[2][1] = (Politics, Local);

>
> > > You could use an object with array properties. With an object literal
> > > you could just print the data structure into the HTML page (assuming
> > > we are talking about the web.)

>
> > > var listA = {
> > > news: ['internacional','asia','europe'],
> > > entertainment: ['showbizz', 'events'],
> > > politics: ['local']

>
> > > };

>
> > > You could dynamically add another category

>
> > > listA.news.push('foo');

>
> > > --------

>
> > > You could use a function to build up the data structure

>
> > > function add(list, cat1, cat2) {
> > > if (!list[cat1]) {
> > > list[cat1] = [];
> > > }
> > > list[cat1].push(cat2);

>
> > > }

>
> > > var listA = {};
> > > add(listA, "News", "Internacional");
> > > add(listA, "News", "Asia") ;
> > > add(listA, "News", "Europe");
> > > add(listA, "Entertainment", "Showbizz");
> > > add(listA, "Entertainment", "Events");
> > > add(listA, "Politics", "Local");

>
> > > > I then would like to iterate through the array using a FOR statment

>
> > > > for(i=0; .... ; i++)
> > > > {

>
> > > > for(j=0; ...; j++)
> > > > {
> > > > ...
> > > > }

>
> > > > }

>
> > > for (var p in listA) { // loop through an object
> > > for (var i =0; i< listA[p]; i++) { // loop through an array
> > > alert(listA[p] + ": " + listA[p][i]);
> > > }

>
> > > }
> > > > How do i create this model using Javascript?

>
> > > You may want to read the first few chapters of a JavaScript language
> > > book. This is the only recommended book.

>
> > > <URL:http://jibbering.com/faq/#FAQ3_1>

>
> > > Peter

>
> > Thank you guys for both of your answers.
> > The reason i want it to create an array is because after creating the
> > array i want to create 2 combobox using the JavaScript DOM and
> > populate the object options with the values of the array

>
> > The first combobox will be populated with the items in LIST A and the
> > second combobox will contain the items associated with selection in
> > the first combobox.

>
> > I already have the DOM function and now i have the array part.
> > For your info, the DOM goes something like this:

>
> > ...
> > var tagForm = document.createElement('form');
> > var tagSelect = document.createElement('select');
> > tagSelect.setAttribute('name', 'Albums');

>
> > for (i=0; i<subalbumsElems.length; i++)
> > {
> > ...
> > var tagOption = document.createElement('option');
> > tagOption.setAttribute('value', i);
> > tagOptionValue = document.createTextNode('....');
> > tagOption.appendChild(tagOptionValue)
> > tagSelect.appendChild(tagOption);

>
> > } //end for

>
> > tagForm.appendChild(tagSelect);
> > ...

>
> Make sure you really cross browser test this. I remember something
> strange when manipulating options in a select using dom functions. If
> you have a problem (or maybe if you just want shorter code) you could
> build HTML for the select with the options nested and then use
> innerHTML to insert the select into the page.
>
> Peter


Thanks Peter.
Definitely i would make sure of the cross-browsing issue. As long as
it works on IE6+, Firefox 1.5+, Safari and Opera. I Don't mind about
IE5/5.5 and the rest).
If not, like you mentionned, i will use the save method and build the
HTML code using strings and pasted using the innerHTML property..

Got to go build this piece of code
Thanks again,
Marco

 
Reply With Quote
 
SM
Guest
Posts: n/a
 
      04-27-2007
On Apr 26, 11:35 pm, Peter Michaux <petermich...@gmail.com> wrote:
> On Apr 26, 8:28 pm, SM <servandomont...@gmail.com> wrote:
>
>
>
> > On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.com> wrote:

>
> > > On Apr 26, 6:24 pm, SM <servandomont...@gmail.com> wrote:

>
> > > > Hello
> > > > I'm trying to create a multi dimensional array in JavaScript, but
> > > > after some reading i still can't figure out how to apply it to my
> > > > model.

>
> > > > Here it is:
> > > > I have a list A and for each item in the list A i want to associate an
> > > > undetermined number of items. The complication for me is the fact that
> > > > the items to associate in list A are undetermined.

>
> > > > ie
> > > > LIST A
> > > > ----------------
> > > > News
> > > > Entertainment
> > > > Politics

>
> > > > Items to associate
> > > > ---------------------
> > > > News ---> Internacional, Asia, Europe,
> > > > Entertainment ---> Showbizz, Events
> > > > Politics ---> Local

>
> > > > i guess i can transalate it like this:

>
> > > > listA[0][0] = (News, Internacional);
> > > > listA[0][1] = (News, Asia) ;
> > > > listA[0][2] = (News, Europe);

>
> > > > listA[1][0] = (Entertainment, Showbizz);
> > > > listA[1][1] = (Entertainment, Events);

>
> > > > listA[2][1] = (Politics, Local);

>
> > > You could use an object with array properties. With an object literal
> > > you could just print the data structure into the HTML page (assuming
> > > we are talking about the web.)

>
> > > var listA = {
> > > news: ['internacional','asia','europe'],
> > > entertainment: ['showbizz', 'events'],
> > > politics: ['local']

>
> > > };

>
> > > You could dynamically add another category

>
> > > listA.news.push('foo');

>
> > > --------

>
> > > You could use a function to build up the data structure

>
> > > function add(list, cat1, cat2) {
> > > if (!list[cat1]) {
> > > list[cat1] = [];
> > > }
> > > list[cat1].push(cat2);

>
> > > }

>
> > > var listA = {};
> > > add(listA, "News", "Internacional");
> > > add(listA, "News", "Asia") ;
> > > add(listA, "News", "Europe");
> > > add(listA, "Entertainment", "Showbizz");
> > > add(listA, "Entertainment", "Events");
> > > add(listA, "Politics", "Local");

>
> > > > I then would like to iterate through the array using a FOR statment

>
> > > > for(i=0; .... ; i++)
> > > > {

>
> > > > for(j=0; ...; j++)
> > > > {
> > > > ...
> > > > }

>
> > > > }

>
> > > for (var p in listA) { // loop through an object
> > > for (var i =0; i< listA[p]; i++) { // loop through an array
> > > alert(listA[p] + ": " + listA[p][i]);
> > > }

>
> > > }
> > > > How do i create this model using Javascript?

>
> > > You may want to read the first few chapters of a JavaScript language
> > > book. This is the only recommended book.

>
> > > <URL:http://jibbering.com/faq/#FAQ3_1>

>
> > > Peter

>
> > Thank you guys for both of your answers.
> > The reason i want it to create an array is because after creating the
> > array i want to create 2 combobox using the JavaScript DOM and
> > populate the object options with the values of the array

>
> > The first combobox will be populated with the items in LIST A and the
> > second combobox will contain the items associated with selection in
> > the first combobox.

>
> > I already have the DOM function and now i have the array part.
> > For your info, the DOM goes something like this:

>
> > ...
> > var tagForm = document.createElement('form');
> > var tagSelect = document.createElement('select');
> > tagSelect.setAttribute('name', 'Albums');

>
> > for (i=0; i<subalbumsElems.length; i++)
> > {
> > ...
> > var tagOption = document.createElement('option');
> > tagOption.setAttribute('value', i);
> > tagOptionValue = document.createTextNode('....');
> > tagOption.appendChild(tagOptionValue)
> > tagSelect.appendChild(tagOption);

>
> > } //end for

>
> > tagForm.appendChild(tagSelect);
> > ...

>
> Make sure you really cross browser test this. I remember something
> strange when manipulating options in a select using dom functions. If
> you have a problem (or maybe if you just want shorter code) you could
> build HTML for the select with the options nested and then use
> innerHTML to insert the select into the page.
>
> Peter


Forgot to mention:
I have a JavaScript book.... the JavaScript Bible book. But it's so
big that sometimes i cant find the answers. I will look in the book
for the object with array properties ..

Thanks

 
Reply With Quote
 
Cah Sableng
Guest
Posts: n/a
 
      04-27-2007
On Apr 27, 10:28 am, SM <servandomont...@gmail.com> wrote:
> On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.com> wrote:
>
>
>
> > On Apr 26, 6:24 pm, SM <servandomont...@gmail.com> wrote:

>
> > > Hello
> > > I'm trying to create a multi dimensional array in JavaScript, but
> > > after some reading i still can't figure out how to apply it to my
> > > model.

>
> > > Here it is:
> > > I have a list A and for each item in the list A i want to associate an
> > > undetermined number of items. The complication for me is the fact that
> > > the items to associate in list A are undetermined.

>
> > > ie
> > > LIST A
> > > ----------------
> > > News
> > > Entertainment
> > > Politics

>
> > > Items to associate
> > > ---------------------
> > > News ---> Internacional, Asia, Europe,
> > > Entertainment ---> Showbizz, Events
> > > Politics ---> Local

>
> > > i guess i can transalate it like this:

>
> > > listA[0][0] = (News, Internacional);
> > > listA[0][1] = (News, Asia) ;
> > > listA[0][2] = (News, Europe);

>
> > > listA[1][0] = (Entertainment, Showbizz);
> > > listA[1][1] = (Entertainment, Events);

>
> > > listA[2][1] = (Politics, Local);

>
> > You could use an object with array properties. With an object literal
> > you could just print the data structure into the HTML page (assuming
> > we are talking about the web.)

>
> > var listA = {
> > news: ['internacional','asia','europe'],
> > entertainment: ['showbizz', 'events'],
> > politics: ['local']

>
> > };

>
> > You could dynamically add another category

>
> > listA.news.push('foo');

>
> > --------

>
> > You could use a function to build up the data structure

>
> > function add(list, cat1, cat2) {
> > if (!list[cat1]) {
> > list[cat1] = [];
> > }
> > list[cat1].push(cat2);

>
> > }

>
> > var listA = {};
> > add(listA, "News", "Internacional");
> > add(listA, "News", "Asia") ;
> > add(listA, "News", "Europe");
> > add(listA, "Entertainment", "Showbizz");
> > add(listA, "Entertainment", "Events");
> > add(listA, "Politics", "Local");

>
> > > I then would like to iterate through the array using a FOR statment

>
> > > for(i=0; .... ; i++)
> > > {

>
> > > for(j=0; ...; j++)
> > > {
> > > ...
> > > }

>
> > > }

>
> > for (var p in listA) { // loop through an object
> > for (var i =0; i< listA[p]; i++) { // loop through an array
> > alert(listA[p] + ": " + listA[p][i]);
> > }

>
> > }
> > > How do i create this model using Javascript?

>
> > You may want to read the first few chapters of a JavaScript language
> > book. This is the only recommended book.

>
> > <URL:http://jibbering.com/faq/#FAQ3_1>

>
> > Peter

>
> Thank you guys for both of your answers.
> The reason i want it to create an array is because after creating the
> array i want to create 2 combobox using the JavaScript DOM and
> populate the object options with the values of the array
>
> The first combobox will be populated with the items in LIST A and the
> second combobox will contain the items associated with selection in
> the first combobox.
>
> I already have the DOM function and now i have the array part.
> For your info, the DOM goes something like this:
>
> ...
> var tagForm = document.createElement('form');
> var tagSelect = document.createElement('select');
> tagSelect.setAttribute('name', 'Albums');
>
> for (i=0; i<subalbumsElems.length; i++)
> {
> ...
> var tagOption = document.createElement('option');
> tagOption.setAttribute('value', i);
> tagOptionValue = document.createTextNode('....');
> tagOption.appendChild(tagOptionValue)
> tagSelect.appendChild(tagOption);
>
> } //end for
>
> tagForm.appendChild(tagSelect);
> ...
>


1. Create a function to change second combo based on first combobox's
selected value.
2. Create a multidimensional array in the way as -Lost or Peter
suggest.
3. Create your first combobox, and fill its options attribute. Point
its change event to function created in no (1).
4. Create your second combobox, with blank options.

HTH

 
Reply With Quote
 
SM
Guest
Posts: n/a
 
      04-27-2007
On Apr 27, 12:02 am, Cah Sableng <cahsabl...@gmail.com> wrote:
> On Apr 27, 10:28 am, SM <servandomont...@gmail.com> wrote:
>
>
>
> > On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.com> wrote:

>
> > > On Apr 26, 6:24 pm, SM <servandomont...@gmail.com> wrote:

>
> > > > Hello
> > > > I'm trying to create a multi dimensional array in JavaScript, but
> > > > after some reading i still can't figure out how to apply it to my
> > > > model.

>
> > > > Here it is:
> > > > I have a list A and for each item in the list A i want to associate an
> > > > undetermined number of items. The complication for me is the fact that
> > > > the items to associate in list A are undetermined.

>
> > > > ie
> > > > LIST A
> > > > ----------------
> > > > News
> > > > Entertainment
> > > > Politics

>
> > > > Items to associate
> > > > ---------------------
> > > > News ---> Internacional, Asia, Europe,
> > > > Entertainment ---> Showbizz, Events
> > > > Politics ---> Local

>
> > > > i guess i can transalate it like this:

>
> > > > listA[0][0] = (News, Internacional);
> > > > listA[0][1] = (News, Asia) ;
> > > > listA[0][2] = (News, Europe);

>
> > > > listA[1][0] = (Entertainment, Showbizz);
> > > > listA[1][1] = (Entertainment, Events);

>
> > > > listA[2][1] = (Politics, Local);

>
> > > You could use an object with array properties. With an object literal
> > > you could just print the data structure into the HTML page (assuming
> > > we are talking about the web.)

>
> > > var listA = {
> > > news: ['internacional','asia','europe'],
> > > entertainment: ['showbizz', 'events'],
> > > politics: ['local']

>
> > > };

>
> > > You could dynamically add another category

>
> > > listA.news.push('foo');

>
> > > --------

>
> > > You could use a function to build up the data structure

>
> > > function add(list, cat1, cat2) {
> > > if (!list[cat1]) {
> > > list[cat1] = [];
> > > }
> > > list[cat1].push(cat2);

>
> > > }

>
> > > var listA = {};
> > > add(listA, "News", "Internacional");
> > > add(listA, "News", "Asia") ;
> > > add(listA, "News", "Europe");
> > > add(listA, "Entertainment", "Showbizz");
> > > add(listA, "Entertainment", "Events");
> > > add(listA, "Politics", "Local");

>
> > > > I then would like to iterate through the array using a FOR statment

>
> > > > for(i=0; .... ; i++)
> > > > {

>
> > > > for(j=0; ...; j++)
> > > > {
> > > > ...
> > > > }

>
> > > > }

>
> > > for (var p in listA) { // loop through an object
> > > for (var i =0; i< listA[p]; i++) { // loop through an array
> > > alert(listA[p] + ": " + listA[p][i]);
> > > }

>
> > > }
> > > > How do i create this model using Javascript?

>
> > > You may want to read the first few chapters of a JavaScript language
> > > book. This is the only recommended book.

>
> > > <URL:http://jibbering.com/faq/#FAQ3_1>

>
> > > Peter

>
> > Thank you guys for both of your answers.
> > The reason i want it to create an array is because after creating the
> > array i want to create 2 combobox using the JavaScript DOM and
> > populate the object options with the values of the array

>
> > The first combobox will be populated with the items in LIST A and the
> > second combobox will contain the items associated with selection in
> > the first combobox.

>
> > I already have the DOM function and now i have the array part.
> > For your info, the DOM goes something like this:

>
> > ...
> > var tagForm = document.createElement('form');
> > var tagSelect = document.createElement('select');
> > tagSelect.setAttribute('name', 'Albums');

>
> > for (i=0; i<subalbumsElems.length; i++)
> > {
> > ...
> > var tagOption = document.createElement('option');
> > tagOption.setAttribute('value', i);
> > tagOptionValue = document.createTextNode('....');
> > tagOption.appendChild(tagOptionValue)
> > tagSelect.appendChild(tagOption);

>
> > } //end for

>
> > tagForm.appendChild(tagSelect);
> > ...

>
> 1. Create a function to change second combo based on first combobox's
> selected value.
> 2. Create a multidimensional array in the way as -Lost or Peter
> suggest.
> 3. Create your first combobox, and fill its options attribute. Point
> its change event to function created in no (1).
> 4. Create your second combobox, with blank options.
>
> HTH


Thanks HTH,

 
Reply With Quote
 
-Lost
Guest
Posts: n/a
 
      04-27-2007
SM wrote:
> On Apr 27, 12:02 am, Cah Sableng <cahsabl...@gmail.com> wrote:
>> On Apr 27, 10:28 am, SM <servandomont...@gmail.com> wrote:
>>
>>
>>
>>> On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.com> wrote:
>>>> On Apr 26, 6:24 pm, SM <servandomont...@gmail.com> wrote:
>>>>> Hello
>>>>> I'm trying to create a multi dimensional array in JavaScript, but
>>>>> after some reading i still can't figure out how to apply it to my
>>>>> model.
>>>>> Here it is:
>>>>> I have a list A and for each item in the list A i want to associate an
>>>>> undetermined number of items. The complication for me is the fact that
>>>>> the items to associate in list A are undetermined.
>>>>> ie
>>>>> LIST A
>>>>> ----------------
>>>>> News
>>>>> Entertainment
>>>>> Politics
>>>>> Items to associate
>>>>> ---------------------
>>>>> News ---> Internacional, Asia, Europe,
>>>>> Entertainment ---> Showbizz, Events
>>>>> Politics ---> Local
>>>>> i guess i can transalate it like this:
>>>>> listA[0][0] = (News, Internacional);
>>>>> listA[0][1] = (News, Asia) ;
>>>>> listA[0][2] = (News, Europe);
>>>>> listA[1][0] = (Entertainment, Showbizz);
>>>>> listA[1][1] = (Entertainment, Events);
>>>>> listA[2][1] = (Politics, Local);
>>>> You could use an object with array properties. With an object literal
>>>> you could just print the data structure into the HTML page (assuming
>>>> we are talking about the web.)
>>>> var listA = {
>>>> news: ['internacional','asia','europe'],
>>>> entertainment: ['showbizz', 'events'],
>>>> politics: ['local']
>>>> };
>>>> You could dynamically add another category
>>>> listA.news.push('foo');
>>>> --------
>>>> You could use a function to build up the data structure
>>>> function add(list, cat1, cat2) {
>>>> if (!list[cat1]) {
>>>> list[cat1] = [];
>>>> }
>>>> list[cat1].push(cat2);
>>>> }
>>>> var listA = {};
>>>> add(listA, "News", "Internacional");
>>>> add(listA, "News", "Asia") ;
>>>> add(listA, "News", "Europe");
>>>> add(listA, "Entertainment", "Showbizz");
>>>> add(listA, "Entertainment", "Events");
>>>> add(listA, "Politics", "Local");
>>>>> I then would like to iterate through the array using a FOR statment
>>>>> for(i=0; .... ; i++)
>>>>> {
>>>>> for(j=0; ...; j++)
>>>>> {
>>>>> ...
>>>>> }
>>>>> }
>>>> for (var p in listA) { // loop through an object
>>>> for (var i =0; i< listA[p]; i++) { // loop through an array
>>>> alert(listA[p] + ": " + listA[p][i]);
>>>> }
>>>> }
>>>>> How do i create this model using Javascript?
>>>> You may want to read the first few chapters of a JavaScript language
>>>> book. This is the only recommended book.
>>>> <URL:http://jibbering.com/faq/#FAQ3_1>
>>>> Peter
>>> Thank you guys for both of your answers.
>>> The reason i want it to create an array is because after creating the
>>> array i want to create 2 combobox using the JavaScript DOM and
>>> populate the object options with the values of the array
>>> The first combobox will be populated with the items in LIST A and the
>>> second combobox will contain the items associated with selection in
>>> the first combobox.
>>> I already have the DOM function and now i have the array part.
>>> For your info, the DOM goes something like this:
>>> ...
>>> var tagForm = document.createElement('form');
>>> var tagSelect = document.createElement('select');
>>> tagSelect.setAttribute('name', 'Albums');
>>> for (i=0; i<subalbumsElems.length; i++)
>>> {
>>> ...
>>> var tagOption = document.createElement('option');
>>> tagOption.setAttribute('value', i);
>>> tagOptionValue = document.createTextNode('....');
>>> tagOption.appendChild(tagOptionValue)
>>> tagSelect.appendChild(tagOption);
>>> } //end for
>>> tagForm.appendChild(tagSelect);
>>> ...

>> 1. Create a function to change second combo based on first combobox's
>> selected value.
>> 2. Create a multidimensional array in the way as -Lost or Peter
>> suggest.
>> 3. Create your first combobox, and fill its options attribute. Point
>> its change event to function created in no (1).
>> 4. Create your second combobox, with blank options.
>>
>> HTH

>
> Thanks HTH,


Um... I think that was "Hope that helped."

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Create a multi-dimensional array Archos Javascript 9 11-25-2011 01:15 AM
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
multi-dimensional arrays to 2-dimensional arrays Wirianto Djunaidi Ruby 2 04-29-2008 07:31 AM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57