用json数据形成表格 [英] Form the the table with json data

查看:50
本文介绍了用json数据形成表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为json数据形成表格.我从句柄中删除了引号,逗号和花括号. 并尝试添加用于geo的表,满足哈希和计数.现在仅能添加空间.

I want to form the table for the json data.I juct removed the quotation,comma and curly braces. and tried to add table for geo ,meeting hash and count.Now able to add only space.

const summary_data=[{Geo: "US West", MeetingHash: "Hold/Uncategorized", count: 65},
    {Geo: "NSU", MeetingHash: "Hold/Uncategorized", count: 9}, 
     {Geo: "US East", MeetingHash: "Hold/Uncategorized", count: 3}];
  
       var str="";
        $.each(summary_data, function (key, entry) {
           str += "<tr><td>\r\n"+JSON.stringify(entry) .replace(/,|\{|\}|\"/g," ") + "\n</td></tr>";
         });
            console.log(str);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

推荐答案

以键作为表头..

const summary_data = [{ Geo: "US West", MeetingHash: "Hold/Uncategorized", count: 65 },
  { Geo: "NSU", MeetingHash: "Hold/Uncategorized", count: 9 },
  { Geo: "US East", MeetingHash: "Hold/Uncategorized", count: 3 }];
  var val = summary_data[0];
  var table = `<table><tr>`;
  Object.keys(val).forEach(function (key) {
     table += `<th>${key}</th>`;
  });
  table += '</tr>';
  for (var i in summary_data) {
     table += '<tr>';
     var val = summary_data[i];
     Object.keys(val).forEach(function (key) {
        table += `<td>${val[key]}</td>`;
     });
     table += '</tr>';
  }
  table += '</table>';
  console.log(table);
  $('body').html(table);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

这篇关于用json数据形成表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆