简体   繁体   English

JavaScript:在循环中附加来自二维数组的数据

[英]JavaScript: Appending data from a 2D array in a loop

I have a 2 dimensional array that I am attempting to append it's values.我有一个二维数组,我正在尝试 append 它的值。 I have provided 2 code snippets;我提供了 2 个代码片段; First one is an example of what I am attempting to accomplish with the second code snippet.第一个是我试图用第二个代码片段完成的一个例子。 The first code snippet me appending data manually for every single index.第一个代码片段我为每个索引手动附加数据。

Essentially I am attempting to create a list that looks like本质上,我正在尝试创建一个看起来像的列表

  1. Online 3.2在线3.2
  2. Appointment 2.9预约 2.9
  3. Store 1.8商店 1.8

which is why in my first code snippe I do a col-md-7 first for the index with the title and then follow up with a col-md-5 for the index with the decimal like this:这就是为什么在我的第一个代码片段中,我首先为带有标题的索引执行col-md-7 ,然后为带有小数的索引执行col-md-5 ,如下所示:

htmlOutput += "<div class='col-md-7'>";
htmlOutput += "<p> 1. " + array[0][0] + "</p>";
htmlOutput += "</div>";

htmlOutput += "<div class='col-md-5 '>";
htmlOutput += "<p>" + array[0][1]+ "</p>";
htmlOutput += "</div>";

In my second code snippet, I am attempting to loop through my array and increment the data but I am unable to seperate the indexs in the array for a example my current result is:在我的第二个代码片段中,我试图遍历我的数组并增加数据,但我无法分隔数组中的索引,例如我当前的结果是:

<div class='col-md-5'><p>Online,3.2</p></div>
<div class='col-md-5'><p>Appointment,2.9</p></div>
<div class='col-md-5'><p>Store,1.8</p></div>
<div class='col-md-5'><p>Date,4.1</p></div>
<div class='col-md-5'><p>Phone,1.2</p></div>

Where index 0 and 1 are being returned together in one div.其中索引 0 和 1 在一个 div 中一起返回。 My expected outcome is to have these indexes separate and have my return be something like this我的预期结果是将这些索引分开并让我的回报是这样的

 <div class='col-md-7'><p>Online</p></div> 
 <div class='col-md-5'><p>3.2</p></div>   // index 0 

<div class='col-md-7'><p>Appointment</p></div> 
 <div class='col-md-5'><p>2.9</p></div>     // index 1 

 let htmlOutputArray = "" const array = [ ["Online", 3.2 ], ["Appointment", 2.9], ["Store", 1.8], ["Date", 4.1], ["Phone", 1.2] ]; let htmlOutput = "" htmlOutput += "<div class='col-md-6'>"; htmlOutput += "<div class='row'>"; htmlOutput += "<div class='col-md-7'>"; htmlOutput += "<p> 1. " + array[0][0] + "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-5 '>"; htmlOutput += "<p>" + array[0][1]+ "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-7'>"; htmlOutput += "<p> 2. " + array[1][0] + "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-5'>"; htmlOutput += "<p>" + array[1][1] + "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-7'>"; htmlOutput += "<p> 3. " + array[2][0] + "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-5'>"; htmlOutput += "<p>" + array[2][1] + "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-7'>"; htmlOutput += "<p> 4. " + array[3][0] + "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-5'>"; htmlOutput += "<p>" + array[3][1] + "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-7'>"; htmlOutput += "<p> 5. " + array[4][0] + "</p>"; htmlOutput += "</div>"; htmlOutput += "<div class='col-md-5'>"; htmlOutput += "<p>" + array[4][1]+ "</p>"; htmlOutput += "</div>"; htmlOutput += "</div>"; htmlOutput += "</div>"; htmlOutputArray += htmlOutput; // concat all previously rendered html outputs $("#appendData").html(htmlOutputArray);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='appendData'> </div>

 let text = "" const array = [ ["Online", 3.2 ], ["Appointment", 2.9], ["Store", 1.8], ["Date", 4.1], ["Phone", 1.2] ]; for (let i = 0; i < array.length; i++) { text += "<div class='col-md-7'>"; text += "<p>" + array[0][i] + "</p>"; text += "</div>"; } console.log(text)

Check out the snippet below, you were passing the i index into the second dimension of the array instead of the first.查看下面的代码片段,您将i索引传递到数组的第二维而不是第一维。

I've switched to template string syntax because it's a bit more concise in this case.我已经切换到模板字符串语法,因为在这种情况下它更简洁一些。

I've also used the i index to output the "1.", "2."我还使用了 output 的i索引“1.”、“2”。 before the titles.在标题之前。

 let text = "" const array = [ ["Online", 3.2 ], ["Appointment", 2.9], ["Store", 1.8], ["Date", 4.1], ["Phone", 1.2] ] for (let i = 0; i < array.length; i++) { text += `<div class='col-md-7'><p>${i + 1}. ${array[i][0]}</p></div>` text += `<div class='col-md-5'><p>${array[i][1]}</p></div>` } // console.log(text) document.getElementById('appendData').innerHTML = text
 <div id='appendData'></div>

i should be the first index, not the second index. i应该是第一个索引,而不是第二个索引。 Then you need to concatenate [i][0] and [i][1] , just like in your expanded code.然后你需要连接[i][0][i][1] ,就像在你的扩展代码中一样。

 let text = "" const array = [ ["Online", 3.2], ["Appointment", 2.9], ["Store", 1.8], ["Date", 4.1], ["Phone", 1.2] ]; for (let i = 0; i < array.length; i++) { text += "<div class='col-md-7'>"; text += "<p>" + array[i][0] + "</p>"; text += "</div>"; text += "<div class='col-md-5 '>"; text += "<p>" + array[i][1] + "</p>"; text += "</div>"; } console.log(text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM