Answer the question
In order to leave comments, you need to log in
[].map and String.prototype.trim
Often you have to do something like this:
function clearTags (rawTags) {
return rawTags
.split(',')
.map(
function (tag) {
return tag.trim()
}
);
}
.map(String.prototype.trim.call)
(does not work), but I did not succeed. Please share if anyone knows. function clearTags (rawTags) {
var trim = function (arg) { return String.prototype.trim.call(arg); };
return rawTags
.split(',')
.map(trim);
}
trivial, not so beautiful and not without flaws.
Answer the question
In order to leave comments, you need to log in
The best solution, of course, is a combination of proposals from egorinsk and Aingis. I’ll add just how to get out with prototypes:
function clearTags(str){
var trim = String.prototype.trim.call.bind(String.prototype.trim);
return str
.split(',')
.map(trim);
}
function clearTags (rawTags) {
var trim = function (arg) { return String.prototype.trim.call(arg); };
return rawTags
.split(',')
.map(trim);
}
function clearTags (rawTags) {
var trim = function (tag) { return tag.trim(); };
return rawTags
.split(',')
.map(trim);
}
In ECMAScript 6, writing lambdas is much easier:
str.split(",").map(function(str) str.trim());//Fx3+
str.split(",").map(str=>str.trim());//Fx22+
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question