} 如何使用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 HelloI''m trying to create a multi dimensional array in JavaScript, butafter some reading i still can''t figure out how to apply it to mymodel. Here it is:I have a list A and for each item in the list A i want to associate anundetermined number of items. The complication for me is the fact thatthe items to associate in list A are undetermined. ieLIST A----------------NewsEntertainmentPolitics Items to associate---------------------News --- Internacional, Asia, Europe,Entertainment ---Showbizz, EventsPolitics ---Locali 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? ThanksMarco 解决方案 SM wrote:HelloI''m trying to create a multi dimensional array in JavaScript, butafter some reading i still can''t figure out how to apply it to mymodel.Here it is:I have a list A and for each item in the list A i want to associate anundetermined number of items. The complication for me is the fact thatthe items to associate in list A are undetermined.ieLIST A----------------NewsEntertainmentPoliticsItems to associate---------------------News --- Internacional, Asia, Europe,Entertainment ---Showbizz, EventsPolitics ---Locali 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 statmentfor(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. ---LostRemove the extra words to reply by e-mail. Don''t e-mail me. I amkidding. No I am not.On Apr 26, 6:24 pm, SM <[email protected]:HelloI''m trying to create a multi dimensional array in JavaScript, butafter some reading i still can''t figure out how to apply it to mymodel.Here it is:I have a list A and for each item in the list A i want to associate anundetermined number of items. The complication for me is the fact thatthe items to associate in list A are undetermined.ieLIST A----------------NewsEntertainmentPoliticsItems to associate---------------------News --- Internacional, Asia, Europe,Entertainment ---Showbizz, EventsPolitics ---Locali 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 literalyou could just print the data structure into the HTML page (assumingwe 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 statmentfor(i=0; .... ; i++){ for(j=0; ...; j++) { ... }}for (var p in listA) { // loop through an objectfor (var i =0; i< listA[p]; i++) { // loop through an arrayalert(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 languagebook. This is the only recommended book. <URL: http://jibbering.com/faq/#FAQ3_1> Peter On Apr 26, 10:45 pm, Peter Michaux <[email protected]:On Apr 26, 6:24 pm, SM <[email protected]: 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 literalyou could just print the data structure into the HTML page (assumingwe are talking about the web.)var listA = { news: [''internacional'',''asia'',''europe''], entertainment: [''showbizz'', ''events''], politics: [''local'']};You could dynamically add another categorylistA.news.push(''foo'');--------You could use a function to build up the data structurefunction 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 languagebook. This is the only recommended book.<URL:http://jibbering.com/faq/#FAQ3_1>PeterThank you guys for both of your answers.The reason i want it to create an array is because after creating thearray i want to create 2 combobox using the JavaScript DOM andpopulate the object options with the values of the array The first combobox will be populated with the items in LIST A and thesecond combobox will contain the items associated with selection inthe 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 appreciatedMarco 这篇关于如何创建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 06:33