Search code examples
javascriptarraysstringsubstr

JavaScript: Extract first letter of every string in array?


I'm quite new in coding, so might be an easy question for you all. What exactly should I do if I have this array and want to just show the first letter of every element of it?

var friends = ["John", "Will", "Mike"];

I wanted to do it with substr method but I just want to know how to do it with a string.


Solution

  • Use Array.map() to iterate the array, take the 1st letter using destructuring, and return it:

    const friends = ["John", "Will", "Mike"];
    const result = friends.map(([v])=> v);
    console.log(result);