How to Remove last Comma?

后端 未结 12 1939
自闭症患者
自闭症患者 2020-12-08 06:58

This code generates a comma separated string to provide a list of ids to the query string of another page, but there is an extra comma at the end of the string. How can I re

12条回答
  •  无人及你
    2020-12-08 07:48

    Write a javascript function :

    var removeLastChar = function(value, char){
        var lastChar = value.slice(-1);
        if(lastChar == char) {
          value = value.slice(0, -1);
        }
        return value;
    }
    

    Use it like this:

    var nums = '1,2,3,4,5,6,';
    var result = removeLastChar(nums, ',');
    console.log(result);
    

    jsfiddle demo

提交回复
热议问题