如何创建多维数组 [英] How to create a Multi dimensional array

查看:57
本文介绍了如何创建多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在尝试用JavaScript创建一个多维数组,但是经过一些阅读后我仍然无法弄清楚如何应用它到我的

型号。


这是:

我有一个清单A和列表中的每个项目A i想要关联

未确定数量的项目。对我来说复杂的是,在列表A中关联的项目未确定。


ie

列表A < br $>
----------------

新闻

娱乐

政治


要关联的项目

---------------------

新闻---国际,亚洲,欧洲,

娱乐--- Showbizz,活动

政治---本地

i guess i可以这样翻译它:


listA [0] [0] =(新闻,国际);

listA [0] [1] =(新闻) ,亚洲);

listA [0] [2] =(新闻,欧洲);


listA [1] [0] =(娱乐,Showbizz );

listA [1] [1] =(娱乐,活动);


listA [2] [1] =(政治,本地);


然后我想使用FOR语句迭代数组


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

{

for(j = 0; ...; j ++)

{

...

}

}


如何使用Javascript创建此模型?


谢谢

Marco

解决方案

SM写道:


你好

我正在尝试创建一个多JavaScript中的维度数组,但是经过一些阅读后我还不知道如何将它应用到我的

模型中。


这是:

我有一个列表A,对于列表A中的每个项目,我想关联一个

未确定数量的项目。对我来说复杂的是,在列表A中关联的项目未确定。


ie

列表A < br $>
----------------

新闻

娱乐

政治


要关联的项目

---------------------

新闻---国际,亚洲,欧洲,

娱乐--- Showbizz,活动

政治---本地


i我可以这样翻译它:


listA [0] [0] =(新闻,国际);

listA [0] [1 ] =(新闻,亚洲);

listA [0] [2] =(新闻,欧洲);


listA [1] [0] = (娱乐,Showbizz);

listA [1] [1] =(娱乐,活动);


listA [2] [1] =(政治) ,本地);


然后我想使用FOR语句迭代数组


for(i = 0; ... 。; i ++)

{

for(j = 0; ...; j ++)

{

......

}

}


如何使用Javascript创建此模型?



listA [0] = [''新闻,国际',''新闻,亚洲',''新闻,欧洲'';

listA [1] = [''...'',''...'',''...''];


for( var i = 0; i< listA.length; i ++)

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

/ / listA [i] [j]


你可能想要使用命名数组。


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


或者更好,甚至还是使用Object和JSON。


-

-Lost

删除多余的单词以通过电子邮件回复。不要给我发电子邮件。我是开玩笑的。不,我不是。


4月26日下午6:24,SM< servandomont ... @ gmail.comwrote:


你好

我正在尝试用JavaScript创建一个多维数组,但是经过一些阅读后我仍然无法理解为
如何将它应用到我的

模型。


这是:

我有一个列表A和每个项目在列表A我想关联

未确定数量的项目。对我来说复杂的是,在列表A中关联的项目未确定。


ie

列表A < br $>
----------------

新闻

娱乐

政治


要关联的项目

---------------------

新闻---国际,亚洲,欧洲,

娱乐--- Showbizz,活动

政治---本地


i我可以这样翻译它:


listA [0] [0] =(新闻,国际);

listA [0] [1 ] =(新闻,亚洲);

listA [0] [2] =(新闻,欧洲);


listA [1] [0] = (娱乐,Showbizz);

listA [1] [1] =(娱乐,活动);


listA [2] [1] =(政治) ,当地);



您可以使用具有数组属性的对象。使用对象文字

,您可以将数据结构打印到HTML页面中(假设我们正在讨论网页的价值。)


var listA = {

news:[''internacional'',''asia'',''europe''],

娱乐:['' showbizz'',''events''],

政治:[''local'']

};


你可以动态添加另一个类别


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


------- -


您可以使用函数来构建数据结构


函数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, 政治,本地);


然后我想使用FOR语句迭代数组


为(i = 0; ....; i ++)

{


for(j = 0; ...; j ++)

{

...

}


}



for(var a in listA) {//循环一个对象

for(var i = 0; i< listA [p]; i ++){//循环数组

alert(listA [ p] +":" + listA [p] [i]);

}

}


如何使用Javascript创建此模型?



您可能需要阅读JavaScript语言的前几章

book。这是唯一推荐的书。


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


Peter


4月26日晚上10点45分,Peter Michaux< petermich ... @ gmail.comwrote:


4月26日下午6:24,SM< servandomont ... @ gmail.comwrote:


Hello

我正在尝试用JavaScript创建一个多维数组,但是经过一些阅读后我还不知道如何将它应用到我的

模型。


这是:

我有一个列表A,列表A中的每个项目我想关联一个

未定项目数。对我来说复杂的是,在列表A中关联的项目未确定。



ie

LIST A

----------- -----

新闻

娱乐

政治


要关联的项目

---------------------

新闻---国际,亚洲,欧洲,

娱乐--- Showbizz,活动

政治---本地


i我猜这可以像这样:


listA [0] [0] =(新闻,国际);

listA [0] [1] =(新闻,亚洲);

listA [0] [2] =(新闻,欧洲);


listA [1] [0] =(娱乐,Showbizz);

listA [1] [1] = (娱乐,活动);


listA [2] [1] =(政治,本地);



您可以使用具有数组属性的对象。使用对象文字

,您可以将数据结构打印到HTML页面中(假设我们正在讨论网页的价值。)


var listA = {

news:[''internacional'',''asia'',''europe''],

娱乐:['' showbizz'',''events''],

政治:[''local'']


};


你可以动态添加另一个类别


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


--- -----


您可以使用函数来构建数据结构


函数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," ;新闻",欧元pe);

add(listA,Entertainment,Showbizz);

add(listA,Entertainment"," Events);

add(listA," Politics"," Local");


然后我想使用a迭代数组FOR statment


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

{


for(j = 0; ...; j ++)

{

...

}


}


for(var a in listA){//循环通过一个对象

for(var i = 0; i< listA [p]; i ++){//循环数组

alert(listA [p] +":" + listA [p] [i]);

}

}


如何使用Javascript创建此模型?



您可能需要阅读JavaScript语言的前几章

book。这是唯一推荐的书。


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


Peter



谢谢大家的两个答案。

我想要它创建一个数组的原因是因为创建了
之后
数组我想使用JavaScript DOM创建2个组合框和

使用数组的值填充对象选项


第一个组合框将填充LIST A中的项目和

第二个组合框将包含与第一个组合框中的选择相关的项目。


我已经拥有了DOM功能,现在我有了数组部分。

对于你的信息,DOM就是这样的:


... 。

var tagForm = document.createElement(''form'');

var tagSelect = document.createElement(''select'');

tagSelect.setAttribute(''name'',''Albums'');

fo r(i = 0; i< subalbumsElems.length; i ++)

{

....

var tagOption = document.createElement(''option'');

tagOption.setAttribute(''value'',i);

tagOptionValue = document.createTextNode(''....'');

tagOption。 appendChild(tagOptionValue)

tagSelect.appendChild(tagOption);


} //结束


tagForm。 appendChild(tagSelect);

....

谢谢大家的帮助!非常感谢

Marco


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

解决方案

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.


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


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


这篇关于如何创建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆