I have a function that grabs the current user on my page. The variable currentUser equals the full name of the user. Sometimes that is just FirstName LastName but other times it is FirstName MiddleName LastName.
I want to be able to split(or slice) currentUser by " " and set variables FirstName and LastName. And if there is a MiddleName I want it to set the variable MiddleName.
Any help would be appreciated!
EDIT:
What I have tried so far has only returned the first letter of the first name and the last letter of the last name. I haven't tried to set the middle name variable yet. See below. Thank you!
function getCurrentUserEmail() {
var requestUri = "../_api/web/currentUser";
var requestHeaders = {"accept": "application/json;odata=verbose"};
$.ajax({
url: requestUri,
contentType: "application/json;odata=verbose",
headers: requestHeaders,
success: function onSuccess(data, request){
var FullName = data.d.Title;
var EmailAddress = data.d.Email;
CurrentUserEmail = EmailAddress;
CurrentUser = FullName;
name = CurrentUser.split(" ");
fName = name[0];
lName = name[1];
},
error: function (error) {}
});
}
I would think that
split
is likely to be the best approach:Here,
firstName
andlastName
work regardless of the length of the array, or how many segments exist, but themiddleName
will be set when there are exactly3
segments.There is likely another way of doing it with
splice
; it would probably look something like this:I just don't think that's as clean honestly.