Search code examples
javascriptarraysprototype

How can we use Array.prototype.map on strings?


I know we can't use Array.prototype.map on strings like

const str = 'MY NAME IS USER';
const result = str.map(c => c); // "Uncaught TypeError: str.map is not a function"
console.log(result);

This will result in error states:

"Uncaught TypeError: str.map is not a function"

But today I came across a code snipet where I can use call Array.prototype on strings

const str = 'MY NAME IS USER';

const x = Array.prototype.map
  .call(str, (c, i) => {
    if (str.indexOf(c, i + 1) == -1 && str.lastIndexOf(c, i - 1) == -1)
      return c;
  })
  .join('');

console.log(x);

In this snippet I couldn't get how Array.prototype.map called on string str.


Solution

  • Object methods work with this. The map function is implemented something like:

    function map(cb) {
        var r = [];
        for (var i = 0; i < this.length; i++) {
             r.push(cb(this[i]));
        }
        return r;
    }
    

    When you invoke a function with .call, you can decide what the this context should be. Even though the this in array methods is usually an array, you're overriding that here. And the way map is implemented—how it uses this—happens to work both for arrays and strings.