简体   繁体   English

从模板字符串的末尾删除“,”逗号

[英]removing the “,” comma from the end of a template string

I am having a problem is that I can see a small "," comma at the end of each option which I wanted to remove:我遇到的问题是我可以在要删除的每个选项的末尾看到一个小的“,”逗号:

 HTML = `<select> ${[1, 2, 3].map(e => (`<option>${e}</option>\\n`).replace(",", ""))} </select>`; console.log(HTML);

TLDR TLDR

How about use join function like如何使用join功能,如

Answer回答

First of all if you console.log [1, 2, 3].map(e => (`<option>${e}</option>\\n`)) you can get this首先,如果你 console.log [1, 2, 3].map(e => (`<option>${e}</option>\\n`))你可以得到这个

const this = ["<option>1</option>", "<option>2</option>", "<option>3</option>"]  // Array

And if you console.log( this ) with template literal.如果你使用模板文字 console.log( this ) 。 It automatic call Array.toString() .它自动调用Array.toString()

console.log(`${["<option>1</option>", "<option>2</option>", "<option>3</option>"]}`) // automatic call toString()

And when you use replace() the array must be array and "," is not generated yet当您使用replace() ,数组必须是数组,并且尚未生成“,”

Example例子

HTML = `<select>
${[1, 2, 3].map(e => (`<option>${e}</option>\n`)).join("")}
</select>`;
console.log(HTML);

.join("") it .join("")

 HTML = `<select> ${[1, 2, 3].map(e => (`<option>${e}</option>\\n`)).join("")}</select>`; console.log(HTML);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM