Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.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/12.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/7/sql-server/27.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
Javascript 如何删除for循环中的最后一个逗号?_Javascript_Arrays_For Loop - Fatal编程技术网

Javascript 如何删除for循环中的最后一个逗号?

Javascript 如何删除for循环中的最后一个逗号?,javascript,arrays,for-loop,Javascript,Arrays,For Loop,输出为: 该行当前为:1。卡特里娜飓风,2。凯文,3岁。文森特 我想去掉“vincent”后面的最后一个逗号 我尝试了if语句,还尝试了.join方法。我不知道如何在代码中实现它们。您可以用值映射前导数字,并用逗号连接数组 函数线性阵列{ 返回数组长度 ?`行当前为:${array.mapv,i=>`${i+1}.${v}`.join','}` :这条线是空的。; } var Lineof People=[katrina,kevin,vincent]; console.logcurrentli

输出为: 该行当前为:1。卡特里娜飓风,2。凯文,3岁。文森特

我想去掉“vincent”后面的最后一个逗号


我尝试了if语句,还尝试了.join方法。我不知道如何在代码中实现它们。

您可以用值映射前导数字,并用逗号连接数组

函数线性阵列{ 返回数组长度 ?`行当前为:${array.mapv,i=>`${i+1}.${v}`.join','}` :这条线是空的。; } var Lineof People=[katrina,kevin,vincent]; console.logcurrentlineofpeople;
console.logcurrentLine[] 您可以用值映射前导数字,并用逗号连接数组

函数线性阵列{ 返回数组长度 ?`行当前为:${array.mapv,i=>`${i+1}.${v}`.join','}` :这条线是空的。; } var Lineof People=[katrina,kevin,vincent]; console.logcurrentlineofpeople;
console.logcurrentLine[] 使用textToPrint.slice0,-1删除返回字符串的最后一部分


使用textToPrint.slice0,-1删除返回字符串的最后一部分

一个简单的解决方案是使用和连接

在这里,你应该注意,这不是一个单一的报价,而是一个倒勾

var Lineof People=[卡特里娜,凯文,文森特] const emptyLine=[] //使用箭头功能;代码很短 const currentLine=line=>{ return!line.length?行为空:`行当前为:${line.mape,i=>`${i+1}.${e}`.join','}` } console.logcurrentlineofpeople//应为:该行当前为:1。卡特里娜飓风,2。凯文,3岁。文森特 console.logcurrentLineemptyLine//expected:该行为空一个简单的解决方案是使用和连接

在这里,你应该注意,这不是一个单一的报价,而是一个倒勾

var Lineof People=[卡特里娜,凯文,文森特] const emptyLine=[] //使用箭头功能;代码很短 const currentLine=line=>{ return!line.length?行为空:`行当前为:${line.mape,i=>`${i+1}.${e}`.join','}` } console.logcurrentlineofpeople//应为:该行当前为:1。卡特里娜飓风,2。凯文,3岁。文森特
console.logcurrentLineemptyLine//expected:该行为空您可以使用正则表达式删除最后一个逗号

$用于匹配字符串的结尾

设line='string1,string2,string3,string4,string5,'
console.logline.replace/,$/g,您可以使用regex删除最后一个逗号

$用于匹配字符串的结尾

设line='string1,string2,string3,string4,string5,'
console.logline.replace/,$/g,只需检查当前索引是否是最后一个,如果是,请不要添加逗号:

function currentLine(katzDeliLine) {
    if (katzDeliLine.length > 0) {
        var textToPrint = "The line is currently: "  

        for (var crrLine = 0;  crrLine < katzDeliLine.length; crrLine++) {

        textToPrint = textToPrint + (crrLine + 1)+". " 
        +katzDeliLine[crrLine]+","

}


return textToPrint.replace(/,\s*$/, "");
} else {
return "The line is empty"
}
}

var lineofpeople = ["katrina", "kevin", "vincent"]
console.log(currentLine(lineofpeople));

只需检查当前索引是否是最后一个索引,如果是,请不要添加逗号:

function currentLine(katzDeliLine) {
    if (katzDeliLine.length > 0) {
        var textToPrint = "The line is currently: "  

        for (var crrLine = 0;  crrLine < katzDeliLine.length; crrLine++) {

        textToPrint = textToPrint + (crrLine + 1)+". " 
        +katzDeliLine[crrLine]+","

}


return textToPrint.replace(/,\s*$/, "");
} else {
return "The line is empty"
}
}

var lineofpeople = ["katrina", "kevin", "vincent"]
console.log(currentLine(lineofpeople));
欢迎你,凯文。求你了,欢迎你,凯文。请
function currentLine(katzDeliLine) {
    if (katzDeliLine.length > 0) {
        var textToPrint = "The line is currently: "  

        for (var crrLine = 0;  crrLine < katzDeliLine.length; crrLine++) {

        textToPrint = textToPrint + (crrLine + 1)+". " 
        +katzDeliLine[crrLine]+","

}


return textToPrint.replace(/,\s*$/, "");
} else {
return "The line is empty"
}
}

var lineofpeople = ["katrina", "kevin", "vincent"]
console.log(currentLine(lineofpeople));
for (var crrLine = 0; crrLine < katzDeliLine.length; crrLine++) {

        textToPrint = textToPrint + (crrLine + 1) + ". " + katzDeliLine[crrLine] + (crrLine != (katzDeliLine.length - 1) ? "," : "")

    }