简体   繁体   English

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

[英]How can I remove the last comma in my for loop?

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;
    } else {
        return "The line is empty"
    }
}

var lineofpeople = ["katrina", "kevin", "vincent"]

Output is: The line is currently: 1. katrina, 2. kevin, 3. vincent, 输出为: The line is currently: 1. katrina, 2. kevin, 3. vincent,

I'm trying to get rid of the last comma after 'vincent' 我试图摆脱“文森特”之后的最后一个逗号

I tried an if statement, and I also tried the .join() method. 我尝试了if语句,还尝试了.join()方法。 I don't know how to implement them in the code. 我不知道如何在代码中实现它们。

You could map the leading numbers with the value and join the array with comma. 您可以将前导数字与该值映射,并用逗号将数组连接起来。

 function currentLine(array) { return array.length ? `The line is currently: ${array.map((v, i) => `${i + 1}. ${v}`).join(', ')}.` : "The line is empty."; } var lineofpeople = ["katrina", "kevin", "vincent"]; console.log(currentLine(lineofpeople)); console.log(currentLine([])); 

An easy solution is using template literals and join() . 一个简单的解决方案是使用模板文字join()

Here you should pay attention that it's not single quote but a backtick . 在这里,您应该注意,它不是单引号,而是反引号

 var lineofpeople = ["katrina", "kevin", "vincent"] const emptyLine = [] // Using arrow function; the code is short const currentLine = (line) => { return !line.length ? 'The line is empty' : `The line is currently: ${line.map((e, i) => `${i + 1}. ${e}`).join(', ')}` } console.log(currentLine(lineofpeople)) // expected: The line is currently: 1. katrina, 2. kevin, 3. vincent console.log(currentLine(emptyLine)) // expected: "The line is empty" 

Remove the last part of the returned string using textToPrint.slice(0,-1) 使用textToPrint.slice(0,-1)删除返回字符串的最后一部分

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.slice(0,-1);
   } 
   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));

You can use regex to remove the last comma 您可以使用正则表达式删除最后一个逗号

$ is for matching the end of the string $用于匹配字符串的结尾

 let line = 'string1, string2, string3, string4, string5,' console.log(line.replace(/,$/g,'')) 

Just check if the current index is the last one, if it is, don't add a comma: 只需检查当前索引是否为最后一个索引,如果是,则不要添加逗号:

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

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

    }

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

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