1
function setup() {
  var names = [];
  var name = {firstname: "", lastname: ""};

  name.firstname = "John";
  name.lastname = "Doe";
  names.push(name);

  name.firstname = "Bill";
  name.lastname = "Smith";
  names.push(name);

  return names;
}

var temp = setup();
print temp[0].firstname;

I can't seem to figure out how to return an array of objects from a function. Any idea where I'm going wrong?

The problem is that the result stored in temp is the following:

 [
   {
     firstname: "Bill",
     lastname: "Smith"
   },
   {
     firstname: "Bill",
     lastname: "Smith"
   }
 ]
5
  • You are returning an array... that's why temp[0] works. What's the issue? Commented Jan 19, 2016 at 20:33
  • What makes you think something is wrong? What's happening? Commented Jan 19, 2016 at 20:34
  • Use document.write( temp[0].firstname ) Commented Jan 19, 2016 at 20:34
  • you are pushing a reference to the same object into the names array, and overwriting the properties of that object. it will mean you have duplicate entries of bill smith in the output array. Commented Jan 19, 2016 at 20:36
  • FYI, aside from the fact that JS doesn't deep copy objects automatically, you can actually shorten your code a good bit by simply using a literal structure. No need for .push(). var names = [{firstname: "john", lastname: "doe"}, {firstname: "bill": lastname: "smith"}]; You can also pass multiple items to .push() at once. Commented Jan 19, 2016 at 20:44

2 Answers 2

6

You can't reuse name to push the "second" object because you haven't created a second object, it is the same object. So name.firstname="Bill" is changing the first object not creating a second one.

function setup() {
  var names = [];

  var name = {};
  name.firstname = "John";
  name.lastname = "Doe";
  names.push(name);

  var name2 = {};
  name2.firstname = "Bill";
  name2.lastname = "Smith";
  names.push(name2);

  return names;
}

var temp = setup();
alert(temp[0].firstname); // Alerts "John"

Sign up to request clarification or add additional context in comments.

Comments

5

There are 2 problems with your code.

  1. To paint in console, you need to use console.log(temp[0].firstname) in place of print

  2. You have a name object and you are updating it only. And as objects are passed by reference, both the instances at index 0 and index 1 are updated and have same value.

You need to update your code to following

function setup() {
      var names = [];
      var n1 = {};
    
      n1.firstname = "John";
      n1.lastname = "Doe";
      names.push(n1);
    
      var n2 = {};
      n2.firstname = "Bill";
      n2.lastname = "Smith";
      names.push(n2);
    
      return names;
    }
    
    var temp = setup();
    console.log(temp[0].firstname);

8 Comments

@Downvoter - reason please
No idea what the downvoters problem was. This is a reasonable answer.
@NikolayErmakov - I was holding back while posting this. But at the time I posted my answer Robert Mckee's answer did not had any explanation. And I wanted to add that. Whats wrong with that
Nothing wrong with this answer, and I've upvoted it as it is actually worded clearer than my own.
I upvoted the answer back
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.