Say I have an array of strings. And I want to prepend each of these strings with a fixed (common) string. I know that in Javascript, strings can be concatenated like strM = str1 + str2 + str3 + ... + strN
or strM = concat(str1, str2, str3, ..., strN)
. Consider this piece of code.
var defImgDirPath = 'res/img/';
$([
'home-icon-dark.png',
'home-icon-light.png'
]).each(function() {
/*
* Prepend each string with defImgDirPath
*/
});
Now I can't do this = defImgDirPath + this;
(I was stupid enough to try)
Also, I tried return (defImgDirPath + this);
but that wouldn't work either.
I am thinking something along the lines of a function like this.prependString(defImgDirPath);
but does such a function exist? If not, how do I write one?
Note: I know it can be done much easily and simply with a for
loop too, but what's the fun in that? :)