感谢您的回答。如果我必须显示3 in。我想迭代循环以在每一行中显示3个元素。
@RequestMapping(value = "/")
public String home() {
return "test";
}
答案 0 :(得分:1)
var arr=['apple','boy','cat','dog','egg','fish','gum','gun','hat','ink','jug','kite','lamb','man'];
for(var i=0;i<arr.length;i+=3){
var row = [];
row.push(arr[i] || '' );
row.push(arr[i+1] || '' );
row.push(arr[i+2] || '' );
row.push("<br>");
document.write(row.join(" "))
}
&#13;
答案 1 :(得分:0)
试试这个:
var arr=['apple','boy','cat','dog','egg','fish','gum','gun','hat','ink','jug','kite','lamb','man'];
for(i=0;i<arr.length;i++) {
if(i != 0 && i%3 === 0) {
document.write('<br />');
}
if(arr[i]==undefined)
break;
else {
document.write(arr[i] + ' ');
}
}
答案 2 :(得分:0)
你挑战了我......好吧,这很简单,很容易阅读:
var arr = ['apple','boy','cat','dog','egg','fish','gum','gun','hat','ink','jug','kite','lamb','man'];
var html = [];//like a string builder
html.push('<table>');
for(let i = 0; i < arr.length; i += 3){
html.push('<tr>');
html.push('<td>', arr[i] ,'</td>');
html.push('<td>', arr[i + 1], '</td>');
html.push('<td>', arr[i + 2], '</td>');
html.push('</tr>');
}
html.push('</table>');
document.write(html.join(''));
td{
border: 1px solid black;
}