Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading