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); 

}


长风秋雁
浏览 117回答 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>

HUWWW

不得不用你的第二个对象改变一些东西,成本。我认为您不能在不同的值上使用相同的键名,您将无法迭代它们。现在你可以做两种方法:&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;where place 是 html 中 div 标签的位置。不是最好的解决方案,但它可以工作,如果你想要的话,也可以放下 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

相关分类

JavaScript