Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var myArray = [];
myArray = ["Tayfur Gazioglu", "tayfurgazioglu"];


function cutName(name)
{

   var cut = name[0].split(" ");
  return cut;
}

var myInfo = {
fullName : cutName[myArray],
skype : myArray[1],
github : 'tayfurgazioglu'
};

document.write(myInfo.fullName);


What I have tried:

There is something wrong with the function cutName! I tried the samething without the function, it works. But when i try like this the value of document.write(myInfo.fullName); is undefined.
Posted
Updated 30-Mar-17 9:08am

fullName : cutName[myArray]

you are calling this function in wrong way. lets stick to the basics and call it something like this
JavaScript
fullName : cutName(myArray[0])
 
Share this answer
 
correction
function cutName(name) {

           var cut = name[0].split(" ")[0];
           return cut;
       }


fullName: cutName(myArray),
 
Share this answer
 
Other CP members already pointed it out the correct way to call the function is
JavaScript
fullName : cutName(myArray)


Another tip I wanted to throw in is the full name formatting, this way the function will return "Firsname, Lastname" VS "Firstname,Lastname"

JavaScript
return cut[0] + ', ' + cut[1]; 
//cut[1] + ', ' + cut[0];
//cut[0] + ' ' + cut[1]

OR, this method, you don't have the control to arrange the name.
JavaScript
return cut.join(", ");
//cut.join(" "); 

Example:

cutname - JSFiddle[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900