1

I'm trying to create a object using my constructor Person, but it doesn't work when I initialize the object directly in the array that uses literal notation.

function Person (name, age) {
    this.name = name;
    this.age = age;
} 

var family = [
    [ new Person("alice", 40)   ],
    [ new Person("bob", 42)     ],
    [ new Person("michelle", 8) ],
    [ new Person("timmy", 6)    ]
];

for (var person in family) {
    console.log(family[person].name);
}

But it just prints undefined four times.

I gotta use this notation:

var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);

So it prints alice, bob, michelle, timmy.

What am I doing wrong?

1
  • If you don't want to use the plain for loop, you can just as easily throw it into a forEach Commented Aug 12, 2015 at 14:54

2 Answers 2

2

You could just use this:

var family = [
     new Person("alice", 40),
     new Person("bob", 42),
     new Person("michelle", 8),
     new Person("timmy", 6)    
];

There isn't any need of enclosing each Person in brackets.

Now you can loop through the items of your array like below:

for (var index=0; index<family.length; index++) {
    console.log(family[index].name);
}

I didn't use the for..in since this exists for another reason:

The for..in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.

For more detais about for...in, please have a look here.

function Person (name, age) {
    this.name = name;
    this.age = age;
} 

var family = [
         new Person("alice", 40),
         new Person("bob", 42),
         new Person("michelle", 8),
         new Person("timmy", 6)    
    ];

for (var index=0; index<family.length; index++) {
    document.write(family[index].name);
    document.write("</br>");
}

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

6 Comments

But does it make my code wrong? It should run anyway, I think.
Yup, it does, because using the extra brackets you define an array in your array. Each person get inserted in an array and all these arrays are inserted in the array called family. I don't think that's your intention.
Your code runs, but you look for the property "name" on an array, not on a Person. see this JSBin: jsbin.com/doyabi/edit?js,console
@Pjetr please try to run the code snippet. Furthermore, I suggested Pedro to not define his array as he has already done. There isn't any need for inner arrays.
@Pjetr ok. not a problem all dude :)
|
1

You're actually creating a 2D array - I think you only want a regular array, then use a regular for loop to iterate:

var family = [
    new Person("alice", 40), //omit the inner arrays, etc./.
];

for (var i = 0; i < family.length; i++) { 
    //check em out
    console.log(family[i].name);
}

3 Comments

Why did you use a different for? The one I used is not recommended?
@PedroMelo -- Yeah - the for in syntax if for iterating over object properties. You have an array of objects, so use a regular for for the array.
@PedroMelo You could also use family.forEach(function (person) {console.log(person.name);});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.