简体   繁体   English

从JavaScript中删除字符串中的最后一个逗号

[英]Remove last comma from a string in JavaScript

I have a JavaScript method to return an array of database objects. 我有一个JavaScript方法来返回数据库对象数组。 The following Kendo UI template allows the items to be returned as a comma-separated string. 以下Kendo UI模板允许将项目作为逗号分隔的字符串返回。

The problem is, a comma is always returned at the end of each string. 问题是,总是在每个字符串的末尾返回逗号。 Here's the code by the way: 这是代码的代码:

    #for(var i = 0; i < Categories.length; i++){#
        #:Categories[i].CategoryName#,
    #}#

Any suggestions? 有什么建议么?

You can use this in plain javascript 你可以在普通的javascript中使用它

Categories.slice(0,-1);

Here Categories is the variable from you want to remove last "," (comma) 这里的类别是你要删除的最后一个变量“,”(逗号)

You can use this template: 您可以使用此模板:

 #for(var i = 0; i < Categories.length; i++){#
      #if(i+1 === Categories.length){#
        #:Categories[i].replace(/,/g,'')#
        #} else{#
        #:Categories[i].CategoryName#
      #}#
    #}#

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

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