javascript在一张表中循环数组和对象数据

我有数组和对象数据,我想将它们一起调用。


var data = [

{"number": "PA1234","name": "John"},

{"number": "JM344","name": "jessi"},

{"number": "ML567","name": "Monty"}

];


var costing= {

"cost": 10,

"cost": 20,

"cost": 30,

};

表格中的显示格式


<pre>

<table>

<tr>

<td>number</td>

<td>name</td>

<td>cost</td>

</tr>

</table>

<pre>

到目前为止我已经完成了,但不知道如何调用对象成本计算


var records=$("<table/>").attr("id","tabs");

$("#table").append(records);

for(var j=0;j<data .length;j++)

{

  var tr="<tr>";

  var td1="<td>"+data [j]["number"]+"</td>";

  var td2="<td>"+data [j]["name"]+"</td>";


  $("#tabs").append(tr+td1+td2+td3); 

}


慕虎7371278
浏览 134回答 2
2回答

慕森卡

<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>&nbsp; &nbsp; &nbsp; &nbsp; <title>&nbsp; &nbsp; &nbsp; &nbsp; </title>&nbsp; &nbsp; &nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #t, tr, th, td{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border: 1px solid black;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </style>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <table id="t" cellpadding="10">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cost&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; </body>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; var number = ['PA1234', 'JM344', 'ML567'], name = ['John', 'Jessi', 'Monty'], costing = [30, 30, 30];&nbsp; &nbsp; &nbsp; &nbsp; for(var i=0;i<3;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#t").append('<tr><td>' + number[i] + '</td><td>' + name[i] + '</td><td>' + costing[i] + '</td></tr>');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></html>

largeQ

必须改变你的第二个对象的一些东西,成本。我不认为你可以在不同的值上使用相同的键名称,你将无法迭代它们。现在您可以采取两种方法:&nbsp;var data = [&nbsp; &nbsp; {"number": "PA1234","name": "John"},&nbsp; &nbsp; {"number": "JM344","name": "jessi"},&nbsp; &nbsp; {"number": "ML567","name": "Monty"}];var costing = {"cost0": 10,"cost1": 20,"cost2": 30,};document.addEventListener("DOMContentLoaded", () => {&nbsp; &nbsp; const place = document.getElementById("place").firstElementChild&nbsp; &nbsp; const table = document.createElement("table")&nbsp; &nbsp; for(let i = 0; i < data.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; let tr = document.createElement("tr")&nbsp; &nbsp; &nbsp; &nbsp; let tdNumber = document.createElement("td")&nbsp; &nbsp; &nbsp; &nbsp; let tdName = document.createElement("td")&nbsp; &nbsp; &nbsp; &nbsp; let tdCost = document.createElement("td")&nbsp; &nbsp; &nbsp; &nbsp; tdNumber.innerText = data[i].number&nbsp; &nbsp; &nbsp; &nbsp; tdName.innerText = data[i].name&nbsp; &nbsp; &nbsp; &nbsp; tdCost.innerText = costing["cost"+i]&nbsp; &nbsp; &nbsp; &nbsp; tr.appendChild(tdNumber)&nbsp; &nbsp; &nbsp; &nbsp; tr.appendChild(tdName)&nbsp; &nbsp; &nbsp; &nbsp; tr.appendChild(tdCost)&nbsp; &nbsp; &nbsp; &nbsp; table.appendChild(tr)&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; place.appendChild(table)})但就我个人而言,我会将您的成本核算对象更改为:&nbsp;var costing2 = [&nbsp; &nbsp; 10,20,30]并将 for 循环更改为:for(let i = 0; i < data.length; i++){&nbsp; &nbsp; let tr = document.createElement("tr")&nbsp; &nbsp; let tdNumber = document.createElement("td")&nbsp; &nbsp; let tdName = document.createElement("td")&nbsp; &nbsp; let tdCost = document.createElement("td")&nbsp; &nbsp; tdNumber.innerText = data[i].number&nbsp; &nbsp; tdName.innerText = data[i].name&nbsp; &nbsp; tdCost.innerText = costing2[i]&nbsp; &nbsp; tr.appendChild(tdNumber)&nbsp; &nbsp; tr.appendChild(tdName)&nbsp; &nbsp; tr.appendChild(tdCost)&nbsp; &nbsp; table.appendChild(tr)}&nbsp;其中place是div标签在html中的位置。这不是最好的解决方案,但它可以工作,如果你想要的话,也可以写下 html 代码:<html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <script src="./file.js"></script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div id="place">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <pre>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </pre>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5