jQuery多个数组循环

时间:2013-10-16 17:31:29

标签: jquery

如何编写此表达式需要一个合适的解决方案。

var ary1 = ["","",""];
var ary2 = ["","",""];
var ary3 = ["","",""];
var ary4 = ["","",""];
var div;
for(var i=1; i<5; i++){
    div += ("<p id='text'"+i+">"+(ary+i)[0]+"</p>");
}

只是寻找一个更好的解决方案,让它工作。没有从ary1,ary2,ary3和ary4获得价值。

4 个答案:

答案 0 :(得分:3)

如果您想以这种方式引用不同的数组,将它们存储在对象中将是一个非常优雅的解决方案。

var arrayDictionary = {
  ary1: ["","",""],
  ary2: ["","",""],
  ary3: ["","",""],
  ary4: ["","",""]
};
for(var i=1; i<5; i++){
    div += ("<p id='text'"+i+">"+ arrayDictionary["ary"+i][0]+"</p>");
}

答案 1 :(得分:0)

var ary1 = ["","",""];
var ary2 = ["","",""];
var ary3 = ["","",""];
var ary4 = ["","",""];
var div;
for(var i=1; i<5; i++){
    div += ("<p id='text'"+i+">"+eval('ary'+i '[0]')+"</p>");
}

答案 2 :(得分:0)

尝试一组数组:

ary = [
  ["","",""],
  ["","",""],
  ["","",""],
  ["","",""]
];

var div = '';
for (var i = 0, len = ary.length; i < len; i++) {
    div += "<p id='text'" + i + ">" + ary[i][0] + "</p>";
}

答案 3 :(得分:0)

为什么不使用2D数组?

    arr=[ary1,ary2,ary3,ary4,ary5];

for(var i=1; i<arr.legnth; i++){
  for (var j=1;j<arr[i].length;j++){
    div += ("<p id='text'"+j+">"+arr[i][j]+"</p>");
  }
}