休斯顿94达拉斯84 没有关联阵列但是在Javascript中,只有数组 ,非数字索引类似于PHP中的关联数组。 - Randy 嗨兰迪, 感谢您的回复。 我需要能够通过名称而不是数字索引来引用键, 匹配[" team1"]等于休斯顿。 你的例子是虽然很有启发性,但帮助我实现了所希望的行为。 顺便说一句,我理解根据定义,JavaScript中没有关联数组, 但为了我的目的,上述结构符合我的需要。 "关联数组"在Javascript中实际上只是对象 ,其中字段名称用作键。 < html> < body> < script type =" text / javascript"> var TEAM1 = 0; var TEAM2 = 1; var games = [ [ {team:" Lakers",得分:78}, {球队:萨克拉门托,得分:88} ], [ {球队:休斯顿,得分:94} , {team:" Dallas",得分:84} ] ]; for( g = 0; g< games.length; g ++){ document.write(games [g] [TEAM1] [" team"] +"< br>" + games [ g] [TEAM1] [" score"] +"< br>"); document.write(games [g] [TEAM2] [" team"] +" < br>" + games [g] [TEAM2] [" score"] +"< br>"); } < / script> < / body> < / html> - I''m trying to output the contents of an array of associative arrays inJavaScript. I''m looking for an equivalent of foreach in PHP.Example:var games = new Array();var teams = new Array();teams["team1"] = "Lakers";teams["score1"] = "78";teams["team1"] = "Sacramento";teams["score2"] = "88";games[0] = teams;var teams = new Array();teams["team1"] = "Houston";teams["score1"] = "94";teams["team1"] = "Dallas";teams["score2"] = "84";games[1] = teams;Desired output:Lakers78Sacramento88Houston94Dallas84I tried (unsuccessfully):document.open();for (theGame in games) {for (i in theGame) {document.writeln(i + " = " + theGame[i] + "<br>");}}document.close();Thanks in advance. 解决方案alert(games[0]);And you will see that games[0] isn''t a copy of the array teams. Youalso have a typo in your array elements, you have team1 twice and noteam2.Redefine your output something like this:var games = new Array();games[0] = ["Lakers","78","Sacramento","88"]games[1] = ["Houston","94","Dallas","84"];for (i in games) {for (j in games[i]){document.write(games[i][j] + " ")}document.write(''<br>'')}Outputs:Lakers 78 Sacramento 88Houston 94 Dallas 84There are no "Associative Arrays" in Javascript though, only arrayswith non-numeric indexes that resemble Associative Arrays in PHP.--Randyalert(games[0]);And you will see that games[0] isn''t a copy of the array teams. Youalso have a typo in your array elements, you have team1 twice and noteam2.Redefine your output something like this:var games = new Array();games[0] = ["Lakers","78","Sacramento","88"]games[1] = ["Houston","94","Dallas","84"];for (i in games) {for (j in games[i]){document.write(games[i][j] + " ")}document.write(''<br>'')}Outputs:Lakers 78 Sacramento 88Houston 94 Dallas 84There are no "Associative Arrays" in Javascript though, only arrayswith non-numeric indexes that resemble Associative Arrays in PHP.--RandyHi Randy,Thanks for your reply.I need the ability to reference a key by name instead of by numeric index,where matches["team1"] equals "Houston".Your example was instructive though and helped me achieve the desiredbehavior.BTW, I understand by definition there''s no associative arrays in JavaScript,but for my purposes the aforementioned structure suits my needs."Associative arrays" in Javascript are actually just Objectsin which the field names are used as keys.<html><body><script type="text/javascript">var TEAM1=0;var TEAM2=1;var games = [[{ team:"Lakers", score:78 },{ team:"Sacramento", score:88 }],[{ team:"Houston", score:94 },{ team:"Dallas", score:84 }]];for (g=0;g<games.length;g++) {document.write(games[g][TEAM1]["team"]+"<br>"+games[g][TEAM1]["score"]+"<br>");document.write(games[g][TEAM2]["team"]+"<br>"+games[g][TEAM2]["score"]+"<br>");}</script></body></html>-- 这篇关于循环遍历关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 00:22