英文:
How to build dynamic html table using data from Map for node.js
问题
I have a Map which had key and values, and I just want to store the map key and values in the html table by using the below code snippet.
Code:
let counter = 0;
let htmlTable = "<table class='table table-hover'>" +
"<thead>" +
"<tr>" +
"<th scope='col'>#</th>" +
"<th scope='col'>Network Call</th>" +
"<th scope='col'>Duration</th>" +
"</tr>" +
"</thead>" +
"<tbody>" +
"<tr>" +
myMap.forEach(function(value, key) {
let array = key.split(":");
"<th scope='row'>" + `${counter++}` + "</th>" +
"<td>" + `${array[0]}` + "</td>" +
"<td>" + `${array[1]}` + "</td>" +
""
}) +
"</tr>" +
"</tbody>" +
"</table>";
Map data:
myMap.forEach (function(value, key) {
console.log("MAP" + key + ' = ' + value);
})
Output:
[0-0] MAP /token?re=prime&deviceId=:login = 555
[0-0] MAP /session-context/v1/bootstrap:login = 132
[0-0] MAP /configs/auth:login = 106
I just want to print the map data inside the <tr> row in the above code. But it returned 'undefined'. When I tried to iterate the above map and it had values, but It's not iterated when it's called inside the HTML let variable.
I'm new to JavaScript, is there any other way to make this work?
英文:
I have a Map which had key and values, and I just want to store the map key and values in the html table by using the below code snippet.
Code:
let counter = 0;
let htmlTable = "<table class='table table-hover'>"+
"<thead>"+
"<tr>"+
"<th scope='col'>#</th>"+
"<th scope='col'>Network Call</th>"+
"<th scope='col'>Duration</th>"+
"</tr>"+
"</thead>"+
"<tbody>"+
"<tr>"+
myMap.forEach(function(value, key) {
let array = key.split(":");
"<th scope='row'>"+`${counter++}`+"</th>"+
"<td>"+`${array[0]}`+"</td>"+
"<td>"+`${array[1]}`+"</td>"+
""
})+
"</tr>"+
"</tbody>"+
"</table>"
Map data:
myMap.forEach (function(value, key) {
console.log( "MAP"+ key + ' = ' + value);
})
Output:
[0-0] MAP /token?re=prime&deviceId=:login = 555
[0-0] MAP /session-context/v1/bootstrap:login = 132
[0-0] MAP /configs/auth:login = 106
I just want to print the map data inside the <tr> row in the above code. But it returned 'undefined'. When I tried to iterate the above map and it had values, but Its not iterated when its called inside the html let variable.
I'm new to the .js and Is there any other way to call this or make it work ?
答案1
得分: 1
以下是您要翻译的内容:
我相信这是您正在寻找的内容:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const myMap = ['1:1','2:2']
let htmlTable = `<table class='table table-hover'>
<thead>
<tr>
<th scope='col'>#</th>
<th scope='col'>Network Call</th>
<th scope='col'>Duration</th>
</tr>
</thead>
<tbody>
<tr>
${(myMap.map((value, key) => {
const array = value.split(":");
const newKey = key+1
return `<th scope='row'>${newKey}</th>
<td>${array[0]}</td>
<td>${array[1]}</td>`
})).join('')}
</tr>
</tbody>
</table>`
console.log(htmlTable)
<!-- end snippet -->
使用map函数返回一个字符串数组,join函数将移除连接字符串后出现的逗号。
英文:
I believe this is what you are looking for:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const myMap = ['1:1','2:2']
let htmlTable = `<table class='table table-hover'>
<thead>
<tr>
<th scope='col'>#</th>
<th scope='col'>Network Call</th>
<th scope='col'>Duration</th>
</tr>
</thead>
<tbody>
<tr>
${(myMap.map((value, key) => {
const array = value.split(":");
const newKey = key+1
return `<th scope='row'>${newKey}</th>
<td>${array[0]}</td>
<td>${array[1]}</td>`
})).join('')}
</tr>
</tbody>
</table>`
console.log(htmlTable)
<!-- end snippet -->
with map you return an array of strings and join will remove the commas that would appear after concatenating the strings.
答案2
得分: 0
这可能适用于您,我尚未测试,但这就是想法。您将第一个静态部分的HTML代码分配给htmlTable
,然后将HTML代码与连接的值动态分配,然后是最终的静态部分。
我猜key
的值是类似于"FOO:BAR"
的字符串。
希望对您有所帮助!
let counter = 0;
let htmlTable = "<table class='table table-hover'>" +
"<thead>" +
"<tr>" +
"<th scope='col'>#</th>" +
"<th scope='col'>网络调用</th>" +
"<th scope='col'>持续时间</th>" +
"</tr>" +
"</thead>" +
"<tbody>" +
"<tr>";
myMap.forEach(function(value, key) => {
let array = key.split(":");
htmlTable +=
"<th scope='row'>"+(counter++) +"</th>"+
"<td>"+array[0]+"</td>"+
"<td>"+array[1]+"</td>";
})
htmlTable += "</tr>"+
"</tbody>"+
"</table>";
英文:
This might work for you, I have not tested it but that is the idea. You give assign the first static part of the html code to htmlTable
, then you dinamically assign the html code with the concatenated values and then final static part.
I am guessing that the key
value is a string that goes something like this "FOO:BAR"
.
Hope it helps!
let counter = 0;
let htmlTable = "<table class='table table-hover'>"+
"<thead>"+
"<tr>"+
"<th scope='col'>#</th>"+
"<th scope='col'>Network Call</th>"+
"<th scope='col'>Duration</th>"+
"</tr>"+
"</thead>"+
"<tbody>"+
"<tr>"
myMap.forEach(function(value, key) => {
let array = key.split(":");
htmlTable +=
"<th scope='row'>"+(counter++) +"</th>"+
"<td>"+array[0]+"</td>"+
"<td>"+array[1]+"</td>"
})
htmlTable += "</tr>"+
"</tbody>"+
"</table>"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论