0

So I'm trying to run a for loop to update several objects variables and using an array of their names but the reference doesn't work and instead appears undefined. I presume this is because the array is a string and not an object and was wondering what the easiest way around this would be.

I've attached some simple code to give you an idea what i'm attempting, cheers.

    <!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
var person1 = {
    firstName : "John",
    lastName  : "Doe",
    age       : 50,
    eyeColor  : "blue"
};

var person2 = {
    firstName : "Bill",
    lastName  : "Smith",
    age       : 32,
    eyeColor  : "brown"
};

var people = ["person","person2"];

document.getElementById("demo").innerHTML =
people[0].age
</script>

</body>
</html>
Lambda
  • 25
  • 6

1 Answers1

2

Your array is not containing references to your objects, but simple strings. Try declaring it like that :

var people = [person1, person2];
krtek
  • 26,334
  • 5
  • 56
  • 84