The javascript array is traversed to table

if you traverse a multi-dimensional array into a table?

`var arr = [{id: "1",name:"11",comment:"1"},{id: "2",name:"22",comment:"22"},{id: "333",name:"333",comment:"333"}];

the final effect is as follows

Mar.14,2021

that's really not what I'm talking about. `/ / first, we arr your object

var len = arr.length;
for(var i = 0 ;i < len;iPP){
    for(var key in arr[i]){
        console.log(key);
        console.log(arr[i][key]);
    }
    
}

`
give it a try?


write you a

 var arr = [{id: "1",name:"11",comment:"1"},{id: "2",name:"22",comment:"22"},{id: "333",name:"333",comment:"333"}];
var str=`
        <table border="4">
            <thead>
                <tr>
                    <th>id</th><th>name</th><th>comment</th></tr>
            </thead>
            <tbody>
            ${
                arr.map(o => {
                    return `<tr>
                                <td>${o.id}</td>
                                <td>${o.name}</td>
                                <td>${o.comment}</td>
                            </tr>`
                }).join('')
            }
            </tbody>
         </table>
         `
document.querySelector('-sharptable-wrap').innerHTML=str

actual effect


this is a simple problem of rendering data to the view. It is recommended that you use a framework for bidirectional data binding such as vue. The code is very concise

.
<table>
  <thead>
    <tr>
      <td>id</td>
      <td>name</td>
      <td>comment</td>
    </tr>
  </thead>
  <tbody>
    <tr v-for="item in arr">
      <td>{{ item.id }}</td>
      <td>{{ item.name }}</td>
      <td>{{ item.comment }}</td>
    </tr>
  </tbody>
</table>
<script>
  var table = new Vue({
    el:"-sharptable",
    data:{
      arr:[{id: "1",name:"11",comment:"1"},{id: "2",name:"22",comment:"22"},{id: "333",name:"333",comment:"333"}]
    }
  })
</script>
Menu