Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDominic Bryan
14,452 PointsHelp With use of objects and arrays.
I have code here and would appreciate some help. I want to cut up the first element in my array (my name). I want to be able to change my object "myData" so that "fullName:" uses my function "cutName", so if I was the then type myData.fullName into console it would display two strings in splitting my name into first and last. as you can see I have accomplished this with the function I'm just not sure how to implement it into my object. any help appreciated.
var myArray = ['Dominic Bryan', 'dombguitar'];
function cutName (str) {
myName = str.split(" ");
};
cutName("Dominic Bryan");
console.log(myName);
var myData = {
fullName: cutName(myArray[0]),
skype: myArray[1],
github: null
};
1 Answer
Andrew McCormick
17,730 PointsI think the best way to accomplish what you are after would be to put cutName as a property of myData Object:
var myArray = ['Dominic Bryan', 'dombguitar'];
var myData = {
fullName: myArray[0],
cutName : function() { return this.fullName.split(" ")},
skype: myArray[1],
github: null
};
console.log( myData.cutName() );