循环遍历关联数组 [英] looping through array of associative arrays
问题描述
我正在尝试在
JavaScript中输出关联数组数组的内容。我正在寻找相当于PHP的foreach。
示例:
var games = new Array();
var teams = new Array();
teams [" team1"] =" Lakers";
teams [" score1"] = 78;
球队[" team1"] ="萨克拉门托" ;;
球队[" score2"] =" 88" ;;
游戏[0] =球队;
var队伍=新阵列();
球队[" team1" ] ="休斯顿" ;;
球队[" score1"] =" 94";
球队[" team1"] =" Dallas" ;;
球队[" score2"] =" 84";
games [1] =球队;
所需输出:
湖人队
78
萨克拉门托
88
休斯顿
94
达拉斯
84
我试过(不成功):
document.open();
for(游戏中的游戏()#g
document. = + theGame [i] +"< br>");
}
}
document.close();
提前致谢。
10月14日下午6:21,Bosconian < nob ... @ nowhere.comwrote:
我正试图在
$ b $中输出关联数组数组的内容b JavaScript。我正在寻找相当于PHP的foreach。
示例:
var games = new Array();
var队伍=新阵列();
球队[" team1"] ="湖人队员队伍;
球队[" score1"] =" 78";
teams [" team1"] =" Sacramento" ;;
teams [" score2"] =" 88" ;
games [0] =球队;
alert(游戏[0]);
你会看到游戏[0]不是副本阵列团队。你
你的数组元素也有拼写错误,你有team1两次没有
team2。
var teams = new Array();
teams [" team1"] =" Houston";
teams [" score1"] =" 94" ;;
球队[" team1"] ="达拉斯" ;;
球队[" score2"] =" 84";
游戏[1] =球队;
所需输出:
湖人队
78
萨克拉门托
88
休斯顿
94
达拉斯
84
我试过(不成功):
document.open();
for(theGame in游戏){
for(我在游戏中){
document.writeln(i +" =" + theGame [i] +"< br>" ;);}
} document.close();
提前致谢。
重新定义这样的输出:
var games = new Array();
games [0] = [" Lakers"," 78"," Sacramento"," 88"]
games [1] = [" Houston", 94,达拉斯,84];
for(我在游戏中){
for(j in games [i ]){
document.write(games [i] [j] +"")
}
document.write(' '< br>'')
}
输出:
湖人78萨克拉门托88 >
休斯顿94达拉斯84
没有关联阵列但是在Javascript中,只有数组
,非数字索引类似于PHP中的关联数组。
-
Randy
" One Dumm Hikk" < on ********* @ ctvea.netwrote in message
news:11 ********************* *@i42g2000cwa.googlegr oups.com ...
10月14日下午6:21,Bosconian < nob ... @ nowhere.comwrote:
>我正在尝试输出
JavaScript中关联数组数组的内容。我正在寻找相当于PHP的foreach。
示例:
var games = new Array();
var teams =新阵列();
球队[" team1"] ="湖人队;队友[" score1"] =" 78";
球队[" team1"] =萨克拉门托;
球队[" score2"] =" 88";
游戏[0] =球队;
提醒(游戏[0]);
你会看到游戏[0]不是副本阵列团队。你
你的数组元素也有拼写错误,你有team1两次没有
team2。
> var teams = new Array();
团队[" team1"] ="休斯顿" ;;
团队[" score1"] =" 94";
团队[ " team1"] =" Dallas";
球队[" score2"] =" 84";
游戏[1] =球队;
>期望输出:
湖人队
78
萨克拉门托
88
休斯顿
94
达拉斯
84
我试过(不成功):
document.open();
for(游戏中的游戏){
for(i in theGame){
.document.writeln(i +" =" + theGame [i] +"< br>");}
} document.close();
谢谢提前。
重新定义这样的输出:
var games = new Array();
games [0] = [" Lakers"," 78"," Sacramento"," 88"]
games [1] = [" Houston", 94,达拉斯,84];
for(我在游戏中){
for(j in games [i ]){
document.write(games [i] [j] +"")
}
document.write(' '< br>'')
}
输出:
湖人78萨克拉门托88 >
休斯顿94达拉斯84
没有关联阵列但是在Javascript中,只有数组
,非数字索引类似于PHP中的关联数组。
-
Randy
嗨兰迪,
感谢您的回复。
我需要能够通过名称而不是数字索引来引用键,
匹配[" team1"]等于休斯顿。
你的例子是虽然很有启发性,但帮助我实现了所希望的行为。
顺便说一句,我理解根据定义,JavaScript中没有关联数组,
但为了我的目的,上述结构符合我的需要。
Bosconian说:
>
我正在尝试在
JavaScript中输出关联数组数组的内容。我正在寻找与PHP相当的foreach。
示例:
var games = new Array();
var teams =新阵列();
球队[" team1"] ="湖人队;队友[" score1"] =" 78";
球队[" team1"] =萨克拉门托;
球队[" score2"] =" 88";
游戏[0] =球队;
var teams = new Array();
团队[" team1"] ="休斯顿" ;;
团队[" score1"] =" 94";
团队[" team1"] = 达拉斯;
球队[" score2"] =" 84";
游戏[1] =球队;
所需输出:
湖人队
78
萨克拉门托
88
休斯顿
94
达拉斯
84
我试过(不成功):
document.open();
for(游戏中的游戏){
for(我在游戏中){
document.writeln( i +" =" + theGame [i] +"< br>");
}
}
document.cl OSE();
"关联数组"在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 in
JavaScript. 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:
Lakers
78
Sacramento
88
Houston
94
Dallas
84
I tried (unsuccessfully):
document.open();
for (theGame in games) {
for (i in theGame) {
document.writeln(i + " = " + theGame[i] + "<br>");
}
}
document.close();
Thanks in advance.
On Oct 14, 6:21 pm, "Bosconian" <nob...@nowhere.comwrote:I''m trying to output the contents of an array of associative arrays in
JavaScript. 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;alert(games[0]);
And you will see that games[0] isn''t a copy of the array teams. You
also have a typo in your array elements, you have team1 twice and no
team2.
var teams = new Array();
teams["team1"] = "Houston";
teams["score1"] = "94";
teams["team1"] = "Dallas";
teams["score2"] = "84";
games[1] = teams;
Desired output:
Lakers
78
Sacramento
88
Houston
94
Dallas
84
I tried (unsuccessfully):
document.open();
for (theGame in games) {
for (i in theGame) {
document.writeln(i + " = " + theGame[i] + "<br>");}
}document.close();
Thanks in advance.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 88
Houston 94 Dallas 84
There are no "Associative Arrays" in Javascript though, only arrays
with non-numeric indexes that resemble Associative Arrays in PHP.
--
Randy
"One Dumm Hikk" <on*********@ctvea.netwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...On Oct 14, 6:21 pm, "Bosconian" <nob...@nowhere.comwrote:>I''m trying to output the contents of an array of associative arrays in
JavaScript. 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;
alert(games[0]);
And you will see that games[0] isn''t a copy of the array teams. You
also have a typo in your array elements, you have team1 twice and no
team2.
>var teams = new Array();
teams["team1"] = "Houston";
teams["score1"] = "94";
teams["team1"] = "Dallas";
teams["score2"] = "84";
games[1] = teams;
Desired output:
Lakers
78
Sacramento
88
Houston
94
Dallas
84
I tried (unsuccessfully):
document.open();
for (theGame in games) {
for (i in theGame) {
document.writeln(i + " = " + theGame[i] + "<br>");}
}document.close();
Thanks in advance.
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 88
Houston 94 Dallas 84
There are no "Associative Arrays" in Javascript though, only arrays
with non-numeric indexes that resemble Associative Arrays in PHP.
--
Randy
Hi 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 desired
behavior.
BTW, I understand by definition there''s no associative arrays in JavaScript,
but for my purposes the aforementioned structure suits my needs.
Bosconian said:>
I''m trying to output the contents of an array of associative arrays in
JavaScript. 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:
Lakers
78
Sacramento
88
Houston
94
Dallas
84
I tried (unsuccessfully):
document.open();
for (theGame in games) {
for (i in theGame) {
document.writeln(i + " = " + theGame[i] + "<br>");
}
}
document.close();
"Associative arrays" in Javascript are actually just Objects
in 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>
--
这篇关于循环遍历关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!