html - 尝试将嵌套的 json 文件填充到 html
问题描述
我希望它看起来像这样,如何在使用减速器时检测链接?在遍历 for 循环时,我想检测最后一列的链接并推送标签
{
"alpha": {
"phone": "11223344",
"city": "nyc",
"state": "ny",
"link": "https://www.google.com/search?q=nyc+pictures&oq=nyc+pictures&aqs=chrome..69i57.3022j0j7&sourceid=chrome&ie=UTF-8"
},
"beta": {
"phone": "11223344",
"city": "nyc",
"state": "ny",
"link":"https://www.google.com/search?q=nyc+pictures&oq=nyc+pictures&aqs=chrome..69i57.3022j0j7&sourceid=chrome&ie=UTF-8"
},
"gamma": {
"phone": "11223344",
"city": "nyc",
"state": "ny",
"link": "https://www.google.com/search?q=nyc+pictures&oq=nyc+pictures&aqs=chrome..69i57.3022j0j7&sourceid=chrome&ie=UTF-8"
}
}
解决方案
您可以使用减少
const data = { "alpha": { "phone": "11223344", "city": "nyc", "state": "ny", "link": "https://www.google.com/search?q=nyc+pictures&oq=nyc+pictures&aqs=chrome..69i57.3022j0j7&sourceid=chrome&ie=UTF-8" }, "beta": { "phone": "11223344", "city": "nyc", "state": "ny", "link":"https://www.google.com/search?q=nyc+pictures&oq=nyc+pictures&aqs=chrome..69i57.3022j0j7&sourceid=chrome&ie=UTF-8" }, "gamma": { "phone": "11223344", "city": "nyc", "state": "ny", "link": "https://www.google.com/search?q=nyc+pictures&oq=nyc+pictures&aqs=chrome..69i57.3022j0j7&sourceid=chrome&ie=UTF-8" } }
document.getElementById("tb").innerHTML = Object.entries(data)
.reduce((acc, ent) => {
acc.push(`<tr><td>${ent[0]}</td>`);
Object.entries(ent[1])
.forEach(([key,val]) => acc.push(
key === 'link' ? `<td><a href="${val}">Go</a></td>` : `<td>${val}</td>`
));
acc.push(`</tr>`);
return acc;
}, []).join('');
<table>
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>City</th>
<th>State</th>
<th>Link</th>
</thead>
<tbody id="tb">
</tbody>
</table>
推荐阅读
- amazon-web-services - 在亚马逊管理的区块链上配置事件
- repository - 序列化导出接口时出错,无法加载转换 [null]:找不到目录
- azure - 在Azure虚拟机上部署网站并在互联网上访问
- unity3d - Unity 提供一个游戏对象 不要在加载时销毁
- algorithm - 它是否遵循某种算法或公式..?
- python - 在 ec2 上安装 tensorrt 5.1.5 时出现未满足的依赖项错误
- java - 分页Spring boot JPA异常:方法必须具有以下返回类型之一
- javascript - 我的页面将被视为 iframe 内容,我想删除滚动
- java - Heroku:如何指定 JVM buildpack 非 java 项目的路径?
- javascript - 为什么当我尝试将 php 变量添加到 sql 查询中时,我在 php 中的 sql 查询不起作用?