How do you return an object from a function?

I am learning JS and I would really appreciate your help. I would like to get higher functions and callbacks if possible.

Task

  • Declare these local variables and make them equal to the corresponding information, firstName, LastName, linkedIn, phone, city .
  • Using string concatenation, create a local variable fullName equal to your first and last names, separated by a space.
  • Create a local variable linkedIn , which is a link to your LinkedIn profile.
  • Create a local info variable, which is an array containing your fullName , linkedIn , phone and city , in that order.
  • Create a local variable education , which is an array containing the name of your college / university, area of ​​study, an integer that is your year (expected). Make sure in that order.
  • Define a createApp function that takes your information and education as arguments in that order. This should return an object containing 2 keys. Key names must match variable names. Set the values ​​of these keys to the corresponding arguments.

Steps 1-5: Local variables created.

(function person() { var firstName = "Rob", lastName = "Johnson", fullName = firstName + ", " + lastName, linkedIn = 'https://www.linkedin.com/in/robjohnson', phone = 3105559288, city = 'Los Angeles', info = [firstName, linkedIn, phone, city], education = ['UWRock','Generals','2017']; })(); 

step 6. return an object containing 2 keys. Key names must match variable names. Set the values ​​of these keys to the corresponding arguments. I have no idea, I suppose this should be dynamic, not rigid.

 function createApp(info, education){ var myObj = {}; return(myObj); }; 

I think I should return something like this with a callback:

 myObj { info:'Rob', education: ['UWRock','Generals','2017'] }; 
+5
source share
2 answers

I have found a solution.

 function list () { var firstName = "Rob", lastName = "Johnson", fullName = firstName + ", " + lastName, linkedIn = 'https://www.linkedin.com/in/robjohnson', phone = 3105559288, city = 'Los Angeles', info = [firstName, linkedIn, phone, city], education = ['UWRock','Generals','2017']; createApp(info,education); } function createApp(info, education){ var emptyObj = {}, strArr = ['info','education'], myArray = [info, education]; for (var i = 0; i < myArray.length; i++){ emptyObj[strArr[i]] = myArray[i]; } console.log(emptyObj); } list(); 

results

 [ Object ] { education: ["UWRock", "Generals", "2017"], info: ["Rob", "https://www.linkedin.com/in/robjohnson", 3105559288,"Los Angeles"] } 

I am trying to find a better approach though.

0
source

Let's say you have the following function

 var GraphicalFilters = (function() { var SelectedFilters = {}; return { initializaAllFilters: function(value1,value2) { SelectedFilters = {}; SelectedFilters.value1 = value1; SelectedFilters.value2 = value2; }, getAllFilters: function() { return SelectedFilters; } }; })(); 

You can get the object through the following:

 GraphicalFilters.initializaAllFilters(1,2); var $filters = GraphicalFilters.getAllFilters(); 
0
source

All Articles