I have an array of strings. I want to trim each string in the array.
I thought using [].map() with ''.trim() would work…
[' a', ' b ', 'c'].map(String.prototype.trim);
…but my console said…
TypeError: String.prototype.trim called on null or undefined
I can’t see any null or undefined values in my array.
String.prototype.trim() and Array.prototype.map() are defined in Chrome 17, which I’m using to test.
Why doesn’t this work? I get the feeling I have overlooked something obvious.
I realise I could loop or drop a function in there. That’s not the point of this question, however.
trimis on theStringprototype, meaning that it expects thethiscontext to be that of a string where as themapmethod onArrayprovides the current array item as the first argument and thethiscontext being the global object.