如何在不提及所有数字和中断标记的情况下水平显示数组?

时间:2018-04-20 05:27:57

标签: javascript html arrays



JSON DUMP: {"id":1,"name":"demo","pixeldata":"[{ \"time\" : \"1\", \"colour\" : \"#ff5357\"},{ \"time\" : \"2\", \"colour\" : \"#2424ff\"},{ \"time\" : \"3\", \"colour\" : \"#ff0d13\"},{ \"time\" : \"4\", \"colour\" : \"#f7ff4a\"},{ \"time\" : \"5\", \"colour\" : \"#fa24ff\"},{ \"time\" : \"6\", \"colour\" : \"#ff3e43\"}]"}
[0:] 

 var days = [
   "Monday",
   "Tuesday",
   "Wednesday",
   "Thursday",
   "Friday",
   "Saturday",
   "Sunday",
 ];
         
 document.getElementById("demo2").innerHTML = days[0] + "<br>" +
                                              days[1] + "<br>" +
                                              days[2] + "<br>" +
                                              days[3] + "<br>" +
                                              days[4] + "<br>" +
                                              days[5] + "<br>" + days[6];
&#13;
body {
  margin: 0;
  padding: 0;
}
            
table {
  border: 1px solid #000;
  border-collapse: collapse;
  width: 500px;
  height: 150px;
  margin: 5px;
}
            
table tr td {
  border:1px solid #000;
  padding-left:10px;
}
            
&#13;
&#13;
&#13;

嗨,这听起来像一个奇怪的问题。有没有办法从[0]到[6]显示而不提及中间数字(1-5)?我尝试过循环,但我不认为我做得对。 for(var i = 0; i&gt; days.length; i ++);

2 个答案:

答案 0 :(得分:5)

您可以使用.join()

document.getElementById("demo2").innerHTML = days.join("<br>");

<强>演示:

var days = [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
];
         
document.getElementById("demo2").innerHTML = days.join("<br>");
body {
    margin:0;
    padding:0;
}

table {
    border:1px solid #000;
    border-collapse: collapse;
    width:500px;
    height:150px;
    margin:5px;
}

table tr td{
    border:1px solid #000;
    padding-left:10px;
}
<h1>Concatenation Challenge</h1>
<table>
    <tr>
        <td>All days of the week</td>
        <td id="demo2"></td>
    </tr>
</table>

<强>文档:

答案 1 :(得分:1)

 var days = [
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
                "Sunday",
            ]
            // From HERE
            var myString = "";
            
            for (var i = 0; i < days.length; i++) {

               myString = myString + days[i] + "<br>";
             }
            document.getElementById("demo2").innerHTML = myString;
            // TO HERE
          body {
                margin:0;
                padding:0;
            }
            
            table {
                border:1px solid #000;
                border-collapse: collapse;
                width:500px;
                height:150px;
                margin:5px;
            }
            
            table tr td{
                border:1px solid #000;
                padding-left:10px;
            }
            
        <h1>Concatenation Challenge</h1>
        <table>
            <tr>
                <td>All days of the week</td>
                <td id="demo2"></td>
            </tr>
        </table>