473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to create a Multi dimensional array

SM
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

Apr 27 '07 #1
10 3929
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.
Apr 27 '07 #2
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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

Apr 27 '07 #3
SM
On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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

Apr 27 '07 #4
On Apr 26, 8:28 pm, SM <servandomont...@gmail.comwrote:
On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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

Apr 27 '07 #5
SM
On Apr 26, 11:35 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 8:28 pm, SM <servandomont...@gmail.comwrote:
On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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

Apr 27 '07 #6
SM
On Apr 26, 11:35 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 8:28 pm, SM <servandomont...@gmail.comwrote:
On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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

Apr 27 '07 #7
On Apr 27, 10:28 am, SM <servandomont...@gmail.comwrote:
On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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

Apr 27 '07 #8
SM
On Apr 27, 12:02 am, Cah Sableng <cahsabl...@gmail.comwrote:
On Apr 27, 10:28 am, SM <servandomont...@gmail.comwrote:
On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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,

Apr 27 '07 #9
SM wrote:
On Apr 27, 12:02 am, Cah Sableng <cahsabl...@gmail.comwrote:
>On Apr 27, 10:28 am, SM <servandomont...@gmail.comwrote:
>>On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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." :-x

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Apr 27 '07 #10
SM
On Apr 27, 12:25 am, -Lost <maventheextrawo...@techie.comwrote:
SM wrote:
On Apr 27, 12:02 am, Cah Sableng <cahsabl...@gmail.comwrote:
On Apr 27, 10:28 am, SM <servandomont...@gmail.comwrote:
>On Apr 26, 10:45 pm, Peter Michaux <petermich...@gmail.comwrote:
On Apr 26, 6:24 pm, SM <servandomont...@gmail.comwrote:
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." :-x

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


I need help on how to get a handle on the second combobox so i can
fill it up with values.
The filling issue, i have no problems (it's the same concept as
filling up the first comobobox).
It's how i tell the first combobox 'onchange' function to get a handle
on the second one and populated.

Here's how the code works:
......

//HERE I CREATE THE 1 AND 2 COMBOBOX USING JavaScript DOM
//I FILL UP THE FIRST COMBOBOX AND CREATE AN EMPTY SECOND COMBOBOX
//the onchange function 'addValueto2Comobobox' in the 1 combobox is
were i attempt to fill up the 2 combobox

function createCombobox(Album)
{
var divHeader = document.getElementById("container_header");

var tagForm = document.createElement('form');
var tagSelect = document.createElement('select');
tagSelect.setAttribute('size', '1');
tagSelect.setAttribute('name', 'album');
tagSelect.onchange = function() { addValueto2Comobobox(Album, i,
this); } //Album= the array containing the values

var i=0;
for (var itemvalue in Album) //loop through the album object
{
var tagOption = document.createElement('option');
tagOption.setAttribute('value', i);
if (!i) { tagOption.setAttribute('selected', 'selected'); } //if this
is the first element, add extra attribute to <tagOption>
tagOptionValue = document.createTextNode(itemvalue);
tagOption.appendChild(tagOptionValue)
tagSelect.appendChild(tagOption);
i++;
}
tagForm.appendChild(tagSelect);

//create a second combobox(empty with no values) for the subalbums
var tagSelect = document.createElement('select');
tagSelect.setAttribute('size', '1');
tagSelect.setAttribute('name', 'subalbum');
tagSelect.onchange = function() { dosomething(this); }
tagForm.appendChild(tagSelect);

divHeader.appendChild(tagForm);
}

//HERE's WHERE IM STUCK... I JUST NEED TO GET A HANDLE ON THE SECOND
COMBOBOX
function addValueto2Comobobox(album, id, obj)
{
.....

?????.options.length = 0; //empty the newly created 2 combobox

var x=0;
for (var p in album) //loop through the album object
{
for (var i=0; i<album[p].length; i++) //loop through the
subalbums
{
?????.options[x] = new Option(album[p][i], i); //add an
'option' value to the second combobox
x++;
}
}
....
}
Need help one more time guys
Maybe i should create a new post for this...

Thanks in advance
Marco

Apr 27 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
5
by: Cant Think Today | last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g 1,2,3,4,5 1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6 I think a bit of code that will iterate over these arrays to print out the...
4
by: Robert P. | last post by:
I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not...
3
by: SQLScott | last post by:
I have looked all over and I cannot find an example or information on passing a multi-dimensional array. Well, that is not true. I found a close example in C++ but it didn't work when I...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
4
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ...
3
by: gihope | last post by:
Can anyone tell me why I am not allowed to bind a two or multi dimensional array to GridView and possibly suggest how they would deal with similar scenarios? The array is simply a two dimensional...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
4
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= | last post by:
Hi, consider the attached code. Serializing the multi-dimensional array takes about 36s vs. 0.36s for the single-dimensional array. Initializing the multi-dimensional array takes about 4s...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.