用Javascipt将json文件中的多个值写入HTML表

omvjsjqw  于 2023-05-30  发布在  Java
关注(0)|答案(3)|浏览(155)

有一个json文件有多个值。我想用Javascript将文件中的值写入HTML表。

举例如下:

Json文件:

{
   "628e191673ae8f7750c62fce": {
     "name": "John",
     "surname": "Smith",
     "age": "24"
   },
   "628e1fd0d27981c6250d886c": {
     "name": "Fred",
     "surname": "Bloggs",
     "age": "32"
   },
   "628e20c805f38641bdc08d7d": {
     "name": "Joe",
     "surname": "Harris",
     "age": "27"
   }
}

然后,该表应如下所示:
| 姓名|姓氏|年龄|
| - -----|- -----|- -----|
| 约翰|史密斯|二十四|
| 弗雷德|布洛格斯|三十二|
| 乔|哈里斯|二十七|
什么是最好的方式来做这与JavaScript?谢谢你。

mrfwxfqh

mrfwxfqh1#

您可以将代码分解为小的函数,这些函数逐行构建表,并在此过程沿着创建HTML字符串,然后将它们添加到DOM中。

const data = {"628e191673ae8f7750c62fce":{name:"John",surname:"Smith",age:"24"},"628e1fd0d27981c6250d886c":{name:"Fred",surname:"Bloggs",age:"32"},"628e20c805f38641bdc08d7d":{name:"Joe",surname:"Harris",age:"27"}};

// Returns a simple cell
function getCell(data) {
  return `<td>${data}</td>`;
}

// Returns an string of cells HTML
// `map` returns an array so we need to `join`
// the elements together
function getRow(data) {
  return data.map(getCell).join('');
}

// Returns a set of rows by `mapping`
// over the values of each object, and creating
// a new row for each object
function getRows(data) {
  return data.map(obj => {
    const values = Object.values(obj);
    return `<tr>${getRow(values)}</tr>`;
  }).join('');
}

// Get the object values from the data
const objs = Object.values(data);

// Get the keys from the first object of `objs`.
// This way we don't have to hard-code the headings
// in the HTML markup
const headers = Object.keys(objs[0]);

// Construct a table using the functions
const table = `
<table>
  <tbody>
    <tr class="headings">${getRow(headers)}</tr>
    ${getRows(objs)}
  </tbody>
</table>
`

// Add everything to a container
document.body.insertAdjacentHTML('beforeend', table);
table { border-collapse: collapse; border: 1px solid #454545; width: 300px;}
.headings { background-color: #efefef; font-weight: 600; text-transform: capitalize; }
td { padding: 0.3em; border: 1px solid #efefef;}

附加文件

ih99xse1

ih99xse12#

好吧,你的JSON有点特别,因为你的数据行不是数组,它们是属性。但你可以这样做...

const jsonData = {
   "628e191673ae8f7750c62fce": {
     "name": "John",
     "surname": "Smith",
     "age": "24"
   },
   "628e1fd0d27981c6250d886c": {
     "name": "Fred",
     "surname": "Bloggs",
     "age": "32"
   },
   "628e20c805f38641bdc08d7d": {
     "name": "Joe",
     "surname": "Harris",
     "age": "27"
   }
};

var firstIteration = true;

// create table element
for(var key1 in jsonData) { /* key1 will be the hex name */
  var row = jsonData[key1];

  // create tr element
  if (firstIteration) {
    for(var key2 in row) { /* key2 will be the attributes of the current row */
      // create th element with key2 as content
    }
    firstIteration = false;
  }
  for(var key2 in row) {
    // create td element with row[key2] as content
  }
  // close tr element
}
// close table element
yb3bgrhw

yb3bgrhw3#

简单的方法:

const jsonfile = {
  "628e191673ae8f7750c62fce": {
    "name": "John",
    "surname": "Smith",
    "age": "24"
  },
  "628e1fd0d27981c6250d886c": {
    "name": "Fred",
    "surname": "Bloggs",
    "age": "32"
  },
  "628e20c805f38641bdc08d7d": {
    "name": "Joe",
    "surname": "Harris",
    "age": "27"
  }
}
const parent = document.getElementById('table');
Object.keys(jsonfile).forEach(element => {
  const {
    name,
    surname,
    age
  } = jsonfile[element]
  let child = `<tr>
                            <th>${name}</th>
                            <th>${surname}</th>
                            <th>${age}</th>
                            </tr>`
  parent.insertAdjacentHTML('beforeend', child);
});
<table id="table">
  <tr>
    <th>Name</th>
    <th>Surname</th>
    <th>Age</th>
  </tr>
</table>

相关问题