How do I get rid of the commas when I try to output an array

问题: I'm trying to output strings in an array as a sentence and in between the words it outputs the comma that's used to separate the strings and its so annoying. let words =...

问题:

I'm trying to output strings in an array as a sentence and in between the words it outputs the comma that's used to separate the strings and its so annoying.

let words = document.getElementById("Words");
let wordAr = ["This" , "is" , "a" , "test"];
let textIn = document.getElementById("Input");
let timer = 0;
let WPM = 0;
let text = document.getElementById("text");

words.innerHTML = wordAr.slice(0,4);

window.addEventListener("keydown", function(key){
    if(key.keyCode == 32){
        for (let i=0; i < wordAr.length; i++){
            WPM++;
        };
        console.log("test");
    };
});

回答1:

You can join() using spaces.

let wordAr = ["This" , "is" , "a" , "test"];
console.log(wordAr.join(' '))


回答2:

You can use the toString method of the array object and afterwards use a regular expression to replace all commas by a space.

let wordAr = ["This" , "is" , "a" , "test"];
console.log(wordAr.toString().replace(/,/g, " "));
  • 发表于 2019-03-15 02:32
  • 阅读 ( 163 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除