递归输出多维数组

abc666 2004-11-05 04:15:35
有这样一个数组:

var menu = new Array()

menu[0] = new Array("0","menu0")
menu[1] = new Array("1","menu1")
menu[2] = new Array("2","menu2")

menu[0][0] = new Array("00","menu00")
menu[0][1] = new Array("01","menu01")
menu[0][2] = new Array("02","menu02")

menu[1][0] = new Array("10","menu10")
menu[1][1] = new Array("11","menu11")
menu[1][2] = new Array("12","menu12")

menu[2][0] = new Array("20","menu20")
menu[2][1] = new Array("21","menu21")
menu[2][2] = new Array("22","menu22")

有无办法递归输出结果成下面的样子:
0,menu0
00,menu00
01,menu01
02,menu02
1,menu1
10,menu10
11,menu11
12,menu12
2,menu2
20,menu20
21,menu21
22,menu22


...全文
133 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
JK_10000 2004-11-05
  • 打赏
  • 举报
回复
再加一级菜单:

<script language=javascript>


var tempSubMenus;
var menu = new Array()

menu[0] = new Array("0","menu0")
menu[1] = new Array("1","menu1")
menu[2] = new Array("2","menu2")

tempSubMenus=new Array();
tempSubMenus[0] = new Array("00","menu00")
tempSubMenus[1] = new Array("01","menu01")
tempSubMenus[2] = new Array("02","menu02")
menu[0].subMenu=tempSubMenus;

tempSubMenus=new Array();
tempSubMenus[0] = new Array("10","menu10")
tempSubMenus[1] = new Array("11","menu11")
tempSubMenus[2] = new Array("12","menu12")
menu[1].subMenu=tempSubMenus;

tempSubMenus=new Array();
tempSubMenus[0] = new Array("20","menu20")
tempSubMenus[1] = new Array("21","menu21")
tempSubMenus[2] = new Array("22","menu22")
menu[2].subMenu=tempSubMenus;

//第三级菜单
tempSubMenus=new Array();
tempSubMenus[0] = new Array("210","menu210")
tempSubMenus[1] = new Array("211","menu211")
tempSubMenus[2] = new Array("212","menu212")
menu[2].subMenu[1].subMenu=tempSubMenus;

var menuStr="";

function printMenu(menuArray)
{
for(var i=0;i<menuArray.length;i++)
{
menuStr=menuStr+"\n"+menuArray[i][0]+","+menuArray[i][1];
if(menuArray[i].subMenu!=null) printMenu(menuArray[i].subMenu);
}
}

printMenu(menu);

alert(menuStr);

</script>
JK_10000 2004-11-05
  • 打赏
  • 举报
回复
<script language=javascript>


var tempSubMenus;
var menu = new Array()

menu[0] = new Array("0","menu0")
menu[1] = new Array("1","menu1")
menu[2] = new Array("2","menu2")

tempSubMenus=new Array();
tempSubMenus[0] = new Array("00","menu00")
tempSubMenus[1] = new Array("01","menu01")
tempSubMenus[2] = new Array("02","menu02")
menu[0].subMenu=tempSubMenus;

tempSubMenus=new Array();
tempSubMenus[0] = new Array("10","menu10")
tempSubMenus[1] = new Array("11","menu11")
tempSubMenus[2] = new Array("12","menu12")
menu[1].subMenu=tempSubMenus;

tempSubMenus=new Array();
tempSubMenus[0] = new Array("20","menu20")
tempSubMenus[1] = new Array("21","menu21")
tempSubMenus[2] = new Array("22","menu22")
menu[2].subMenu=tempSubMenus;

var menuStr="";

function printMenu(menuArray)
{
for(var i=0;i<menuArray.length;i++)
{
menuStr=menuStr+"\n"+menuArray[i][0]+","+menuArray[i][1];
if(menuArray[i].subMenu!=null) printMenu(menuArray[i].subMenu);
}
}

printMenu(menu);

alert(menuStr);

</script>
lilyfly 2004-11-05
  • 打赏
  • 举报
回复
数组的生成已经是固定的吗?可不可以改变一下输出的格式啊?
abc666 2004-11-05
  • 打赏
  • 举报
回复
没有你想的这么简单,你试试看就知道了
梅雪香 2004-11-05
  • 打赏
  • 举报
回复
没用过js的数组,不过既然数组都有了,值也清楚,最大不了是一行一行的写输出呗
相关推荐
发帖
JavaScript

8.7w+

社区成员

Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
帖子事件
创建了帖子
2004-11-05 04:15
社区公告
暂无公告