cut the unwanted tail [ 1043 views ]
There is a big array and the items joined by comma like this:
var arr = [...];
var joined = arr.join(',');
console.log(joined);
-> a,w,e,,,,q,,t,z,,,,,,,,,,,
I don’t need the end with the many unwanted comma. I try to cut the string at the last real value.
Let’s extend the string object with the followings:
if (!String.prototype.dropFromEnd) {
Object.defineProperty(String.prototype, 'dropFromEnd', {
enumerable: false,
configurable: false,
writable: false,
value: function (searchString) {
var regEx = new RegExp(searchString + '+$', 'g');
return this.replace(regEx,"");
}
});
};
to call this:
var joined = 'a,w,e,,,,q,,t,z,,,,,,,,,,,'.dropFromEnd(',');
console.log(joined);
-> a,w,e,,,,q,,t,z


