Array of initials for full names in JavaScript

const nameList = ['John Doe', 'Jack Lock', 'Nick Load'];
const initials = nameList.map([n]) => n);
console.log(initials);

I'm attempting to display the initials of the array items, but I'm only able to retrieve the first letter of each item.

This is the current output
https://i.sstatic.net/PQ6g5.png

Desired output

['J.D', 'J.L', 'N.L']

Answer №1

  • Utilize map() function to iterate through the array
  • Use split() method to separate first and last names
  • Apply map() function again on both names
  • Use substring() method to extract the first character
  • Finally, concatenate the initials using the join() method

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"
]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can be taken to resolve the issue of receiving the error message "Invalid 'code' in request" from Discord OAuth2?

I'm in the process of developing an authentication application, but I keep encountering the error message Invalid "code" in request when attempting to obtain a refresh token from the code provided by Discord. Below is a snippet of my reques ...

Adding an external JavaScript file to an HTML document by importing an array

Having trouble loading an array from an external JS file into my HTML. Snippet from js.js: var temp_max = [4,9,2,5,8,4,2,10]; In the HTML: Note: Be sure to download DateJS and place it in "DATE-JS"!! <!doctype html> <html> ... (HTML c ...

An error of undefined Angular Service/Factory has occurred

I created a factory service called siteCollection: spApp.factory('siteCollection', function(){ return { usersObject : [], getUsers : function (){ $().SPServices({ operation: "GetUserCollectionFromSite", completef ...

Navigating additional information in D3 Node Link Force diagram

Recently delving into the world of D3, I have been pleasantly surprised by its capabilities and decided to experiment with the Directional Force Layout. My Objective Initially, I successfully created a json object using a for loop to prepare my items for ...

Struggling to incorporate logout feature with node and passport js

Currently delving into the world of node js, I am in the process of creating a boilerplate utilizing passport js, react, and redux. The issue at hand involves trouble implementing log out functionality as my attempts to log out have been unsuccessful. Anyo ...

Generating HTML tables with charts using FireFox

I am encountering an issue: My table contains charts and tables that are displayed correctly in browsers. However, when I attempt to print it (as a PDF) in Mozilla Firefox, the third speedometer gets cut off, showing only 2.5 speedometers. Using the "s ...

Dynamic JSX tag including attributes

In my project, I have a set of components stored in a folder named systemStatus. These components are accessible through an index.js file as follows: export UserCount from './UserCount' Additionally, I have a JSX component called Status which i ...

How can you add an error notification to a click on a protractor?

Is there a method to associate an error message with a protractor click function? I am envisioning something like the example line below: button.click('Button not clickable'); Currently, when an element cannot be located, I receive the non-spec ...

How to bypass or exclude a value in a numpy array?

I am working with pixel values stored in a h5 file, extracting the data and creating a histogram using numpy. The array contains a specific no-data value of 99999, whereas the rest of the data ranges from -40 to 20. To ensure the no-data value does not aff ...

Exploring ways to loop through objects in a React application

I'm trying to figure out how to modify the code below to iterate through a custom object I have created. function itemContent(number) { return ( <div > <div className="item"> <div className="itemPic& ...

Choosing onSelect is quicker than opting for click

Utilizing the autosuggestion plugin with the onSelect option that changes values in other fields is causing an issue. Everything works fine initially when selecting an item, but when clicking on the input field with the .auto class for the second time (whe ...

Creating a progress bar with a mouse listener in React Material Design

I encountered an issue while working with React and React Material-UI components. Here is what I'm trying to achieve: 1) When the user clicks a button in my component, I want to add a mousemove listener to the page and display a ProgressBar. 2) As ...

Deciphering JSON data results in string output

When I fetch data from a text file in JSON format, the contents of the file look like this: { "id":"tag:search.twitter.com,2005:181865366610382848", "body":"No one wants to carry laptops any more, but we lose iPads and they are not secure. The Sweden ...

Delete the span element if the password requirements are satisfied

I am implementing password rules using span elements. I aim to dynamically remove each span that displays a rule once the input conditions are met. Currently, I have succeeded in removing the span related to the minimum length requirement but I am unsure h ...

Modify the parent scope variable within an Angular directive that contains an isolated scope

Here is a directive implementation: <some-directive key="123"></some-directive> This directive code looks like this: angular.module('app').directive('someDirective', ['someFactory', '$compile', '$ ...

Clicking on the ajax tab control in asp.net displays a single rectangular box - how can this be removed?

When using tab control in Ajax, I encountered an issue where a blue rectangle box appeared when clicking or opening the page. How can I remove this unwanted box? ...

Cease the cropping function for Cropper.js when it extends beyond the boundaries of

Is there a way to prevent the crop corners from moving once the user drags the cursor out of the image while cropping? I encountered an issue where the cropped corners are displaced from the cursor when the user moves out of the image and then back in, wh ...

What is the best way to display items within a table using React?

I'm just starting to learn React. Can someone show me how to use the "map" function to list elements from two different arrays in two columns? state = { dates: ["2000", "2001", "2002"], cases: ["1", "2", "3"] } render() { return ( <thea ...

Struggling to locate the correct setup for .babel and react-hot-loader

I am currently utilizing babel 7. In their documentation, they specify that the new naming convention for plugins should include the @babel/ prefix. The recommended React-hot-loader babelrc configuration is as follows: { "plugins": ["react-hot-loader/ ...

In PHP, it is essential to always complete the necessary information in form validation

I've been working on implementing JavaScript form validation, but I seem to be having trouble with testing for empty fields in the form. Whenever I submit a fully filled out form, it keeps asking me to fill in the blank fields. Here is the code I hav ...