anson920520 anson920520 - 1 month ago 9
Javascript Question

Remove the last element's specific text

For example i have a for loop which will generate some kind of words

for(var i=0;i<profile.test.length;i++){
var wordList = wordList + profile.test[i].text.map(function(k) {
return k.split(/,\s*/).join('+');
}).join(' , ') + '|';
};


and this will return something like

hhh1+hhhh2|fdsg+gsdg , 123+1232|


but i want to remove the last "|"

hhh1+hhhh2|fdsg+gsdg , 123+1232


how could i do that?

Answer

You can use the substr method of the generated String to remove the last character:

wordList = wordList.substr(0, wordList.length -1);
Comments