I would like to extract initials from a string, like:
Name = FirstName LastName
Initials = FL
I can get the above result using this,
const initials = item
.FirstName
.charAt(0)
.toUpperCase() +
item
.LastName
.charAt(0)
.toUpperCase();
But now my requirements are changed as if name only consist of 1 word or more then 2, so in following cases how can I get initials as per my requirements,
FullName = FU
FirstName MiddleName LastName = FL
1stName 2ndName 3rdName 4thName 5thName = 15
How can I get above initials from a string in JS?
Also now I only have item.Name
string as an input
Why no love for regex?
Updated to support unicode characters and use ES6 features
just updated Andrea’s version:
if string includes LastName, just change names.length to names.length-1 to ignore lastname
Easier with map function:
You can do a function for that:
Fiddle: http://jsfiddle.net/5v3n2f95/1/
Check the
getInitials
function below:The functions split the input string by spaces:
Then get the first name, and get the first letter:
If there are more then one name, it takes the first letter of the last name (the one in position
names.length - 1
):You can do something like this;
You can then use the function like so;
I hope this helps.
You can use this shorthand js
To get only First name and Last name you can use this shorthand function
Using some es6 functionality:
You use below one line logic:
This solution uses Array capabilities, Arrow function and ternary operator to achieve the goal in one line.
If name is single word, just take first two chars, but if more, then take 1st chars of first and last names.
(thanks omn for reminding single word name use case)
This should work for majority of the cases including middle names and first name only (extension on @njmwas answer).
I saw a bunch of overcomplicated ways to do this.
I’m really more into simplifying things as much as possible, and enhance things using composition or currying.
Here are my 2 cents:
For your specific case I would suggest something like this:
I hope that helps =D
To get the first name and last name initials, try using the function below.
WHAT HAPPENED: The function splits the incoming string, ignores any name between the first & last names and returns their initials. In the case a single name is entered, a single initial is returned. I hope this helps, cheers.
Use initials, it handles most of the cases and has covered all senarios of a name.
to check how it works visit, try adding your name on https://github.com/gr2m/initials
Thanks
Easy way using ES6 Destructering:
Just surprised that none of the answers put
reduce
to good use.Using
Array.reduce()
, you could do something like –Run the snippet below to check the initials for different use cases –
Note: This one is for a widely used case for displaying names in Avatars, where you wouldn’t want first name initial to be repeated twice and want to restrict the initials to a max of 2 letters
As your full name can have any number of words(middle names) in it, our best bet is to spit it into an array and get the initial characters from the first and last words in that array and return the letters together.
Also if your ‘fullName’ contains only one word, word at
array[0]
andarray[array.length - 1]
would be the same word, so we are handling that if the firstif
.Sample outputs :
> nameToInitials('Prince')
// "P"> nameToInitials('FirstName LastName')
// "FL"> nameToInitials('1stName 2ndName 3rdName 4thName 5thName')
// "15"
Shortest One Liners:
Get First and Last Initial:
John Doe Smith => JS
Get All Initials:
"John Doe Smith" => "JDS"
Get First and Last except get First 2 in case there is only first. (OP’s question)
John => JO
and"John Doe Smith" => "JS"
Note: If the name contains
,
or other non word characters, you might use/w
instead of/S
or sanitize it beforehandOne more way to do the same.
cvs propecia price – http://finasteridepls.com/ propecia hairline