我有一个巨大的JSON文件,其中包含不同的字段,某些字段可能包含URL,我想使所有URL都可单击。有没有一种方法可以使所有网址都可点击,而无需通过其字段访问数据?
这是我的JSON文件的示例:
{
"example": [
{
"name": "google",
"desc": "this is google (https://www.google.com/) "
},
{
"name": "yahoo",
"url": "https://au.yahoo.com/"
},
{
"name": "stackoverflow",
"link": " click this, https://stackoverflow.com"
}
]
}
答案 0 :(得分:1)
尝试
http://www.myportal.com/login
在此处使用标准的JS / HTML功能map,template literals,arrow functions,regexp match,string join,or operator和{{3 }}
let r=d.example.map(x=> (x.u=x.url||x.link||x.desc, x))
.map(x=> (x.u=x.u.match(/http[^ )(]*/),x))
.map(x=> `<div><a href="${x.u}"> ${x.name}</a></div>`)
msg.innerHTML = r.join('')
let d={
"example": [
{
"name": "google",
"desc": "this is google (https://www.google.com/) "
},
{
"name": "yahoo",
"url": "https://au.yahoo.com/"
},
{
"name": "stackoverflow",
"link": " click this, https://stackoverflow.com"
},
]
}
let r=d.example.map(x=> (x.u=x.url||x.link||x.desc, x))
.map(x=> (x.u=x.u.match(/http[^ )(]*/),x))
.map(x=> `<div><a href="${x.u}"> ${x.name}</a></div>`)
msg.innerHTML = r.join('')