anson920520 anson920520 - 10 months ago 49
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] {
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 Source

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

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