-2
const name = ['John Doe', 'Jack Lock', 'Nick Load'];
const initials = name.map([a]) => a);
console.log(initials);

I'm trying to print array items initials, but I only got first letter of the items.

This is what I'm getting
enter image description here

What I need

['J.D', 'J.L', 'N.L']
7
  • 1
    So, instead of destructuring the string and getting the first char, get the full name, split on the whitespace and then get the first letter of each. Commented Nov 22, 2022 at 13:20
  • 1
    I don't see how you get "P A I" from this code. The code isn't even trying to create any initials… Commented Nov 22, 2022 at 13:20
  • There isn't even a p in any of those names ;) Commented Nov 22, 2022 at 13:22
  • Duplicate of Getting Name initials using JS Commented Nov 22, 2022 at 13:24
  • How much research effort is expected of Stack Overflow users? Commented Nov 22, 2022 at 13:24

1 Answer 1

1
  • map() to loop over the array
  • split() to get first and last name
  • map() again over both names
  • substring() to get the first char
  • join() to concat again

const name = ['John Doe', 'Jack Lock', 'Nick Load'];

const initials = name.map(n => n.split(' ').map(s => s.substring(0, 1)).join('.'));

console.log(initials);

[
  "J.D",
  "J.L",
  "N.L"
]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.