简体   繁体   English

我如何摆脱这个数组中的逗号?

[英]How do I get rid of the comma in this array?

 const el_Demo = document.getElementById("demo") // demo element function myFunction() { const stringSample = "how was your day?"; const arrayObject = stringSample.split(" ", 2); el_Demo.textContent = arrayObject; const textValue = el_Demo.textContent; el_Demo.textContent = textValue.toUpperCase(); }
 <p id="demo"> ... result after click ...</p> <button class="btn" onclick="myFunction()">click</button>

If I split the comma , then the code results in the full words I've written in the str variable , and not only 2.如果我拆分逗号,那么代码会生成我在 str 变量中写入的完整单词,而不仅仅是 2 个。
How do I get rid of the comma that appears?如何摆脱出现的逗号?

If you join it back with res.join(" ");如果你用res.join(" "); joinyou will convert it from an array to a string with no comma:您会将其从数组转换为不带逗号的字符串:

document.getElementById("demo").innerHTML = res.join(" ");

You can use join(' ') to join the array.您可以使用join(' ')加入数组。

 function myFunction() { var str = "how was your day?"; var res = str.split(" ", 2); document.getElementById("demo").innerHTML = res.join(' '); var txt = document.getElementById("demo").innerHTML; document.getElementById('demo').innerHTML = txt.toUpperCase(); }
 <div id="demo"></div> <button class="btn" onclick="myFunction()">click</button>

Which produces其中产生

HOW WAS

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

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