Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Join不从JavaScript数组中删除逗号_Javascript_Arrays - Fatal编程技术网

Join不从JavaScript数组中删除逗号

Join不从JavaScript数组中删除逗号,javascript,arrays,Javascript,Arrays,我想删除像这样出现的逗号。我试着加入,但不起作用 下面是JavaScript <script> function shuffle(a) { for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [a[i], a[j]] = [a[j], a[i]]; } return a; } var arr =

我想删除像这样出现的逗号。我试着加入,但不起作用

下面是JavaScript

<script>
function shuffle(a) {
    for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [a[i], a[j]] = [a[j], a[i]];
    }
    return a;
}

var arr = [
        '<p><em>"Comment 1"</em> - <strong>Name 1</strong></p>',
        '<p><em>"Comment 2"</em> - <strong>Name 2</strong></p>',
        '<p><em>"Comment 3"</em> - <strong>Name 3</strong></p>',
        '<p><em>"Comment 4"</em> - <strong>Name 4</strong></p>',
        '<p><em>"Comment 5"</em> - <strong>Name 5</strong></p>',
        '<p><em>"Comment 6"</em> - <strong>Name 6</strong></p>'
]

/* note: the javascript that updates the div had to be near the end
 * of the body to work (probably just after the div)
 */
arr.join('');
shuffle(arr);
document.getElementById("randoms").innerHTML = arr.slice(0,3).toString();
</script>

函数洗牌(a){
对于(设i=a.length-1;i>0;i--){
常数j=数学楼层(数学随机()*(i+1));
[a[i],a[j]=[a[j],a[i]];
}
返回a;
}
var arr=[
““注释1”-名称1

”, ““评论2”-名称2

”, ““评论3”-命名3

”, ““注释4”-名称4

”, ““注释5”-名称5”, ““评论6”-命名6

” ] /*注意:更新div的javascript必须接近末尾 *要工作的身体(可能就在div之后) */ arr.join(“”); 洗牌(arr); document.getElementById(“randoms”).innerHTML=arr.slice(0,3).toString();
这是HTML

<div id="randoms">
</div>

您的
。加入
不做任何事情:

arr.join('');
它通过用空字符串连接所有元素来创建一个字符串,但结果字符串未被使用。原始数组仍然是一个数组,因此稍后调用
arr.slice(0,3).toString()
时,
toString
会在元素之间插入逗号

toString
更改为
.join(“”)

函数洗牌(a){
对于(设i=a.length-1;i>0;i--){
常数j=数学楼层(数学随机()*(i+1));
[a[i],a[j]=[a[j],a[i]];
}
返回a;
}
var arr=[
““注释1”-名称1

”, ““评论2”-名称2

”, ““评论3”-命名3

”, ““注释4”-名称4

”, ““注释5”-名称5”, ““评论6”-命名6

” ] 洗牌(arr); document.getElementById(“randoms”).innerHTML=arr.slice(0,3).join(“”)
Array.prototype.join
通过将数组中的所有元素连接起来并返回一个新字符串,该字符串由数组中的字符串分隔

现在,您刚刚调用了分配给
Array().slice().toString()
join
函数

Array.toString()
通过使用逗号连接元素来返回新字符串

应替换为
arr.slice(0,3).join(“”)

函数洗牌(a){
对于(设i=a.length-1;i>0;i--){
常数j=数学楼层(数学随机()*(i+1));
[a[i],a[j]=[a[j],a[i]];
}
返回a;
}
var arr=[
““注释1”-名称1

”, ““评论2”-名称2

”, ““评论3”-命名3

”, ““注释4”-名称4

”, ““注释5”-名称5”, ““评论6”-命名6

” ] /*注意:更新div的javascript必须接近末尾 *要工作的身体(可能就在div之后) */ 洗牌(arr); document.getElementById(“randoms”).innerHTML=arr.slice(0,3).join(“”)
join
将数组转换为字符串,但不会改变数组。