gpt4 book ai didi

javascript - 使json中的url可点击

转载 作者:行者123 更新时间:2023-11-30 19:34:06 24 4
gpt4 key购买 nike

我有一个巨大的 JSON 文件,其中包含不同的字段,一些字段可能包含一个 URL,我想让所有的 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"
}
]
}

最佳答案

尝试

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('')

W在这里使用标准的JS/HTML特性map , template literals , arrow functions , regexp match , string join , or operatorinnerHTML

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('')
<div id="msg"></div>

关于javascript - 使json中的url可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56129722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com