//map循环数组,判断是否包含某关键字并替换
let webs = [{
website: "尽享网",
url: "https://www.jinshare.cn"
},
{
website: "转笔客",
url: "http://www.zhuanbike.com"
},
{
website: "百数通",
url: "http://www.30725.com"
},
{
website: "",
url: ""
},
,
{
website: "",
url: ""
}
];
function template() {
return `<ul>
${webs.map(
item=>
links`<li>网站:${item.website},网址${item.url}</li>`//links为标签模板
).join("")}
</ul>
`
}
function links(string, ...vars) { //links中分别代表,links标签里面的字符串,变量
console.log(string);console.log(...vars)
return string.map((str, key) => {
return (
str + (vars[key] ? vars[key].replace("尽享网", `<a href="https://www.jinshare.cn">尽享网</a>`) : "")
)//var的key是循环来自string的下标,本身...var就不需要循环了, 但是避免下表无数据,故三元判断一下
}).join("");
}
document.body.innerHTML += template();