I'm trying to create a simple table with jQuery by looping through an array of objects but get this message in the console:
TypeError: 'undefined' is not an object (evaluating 'people[i].lastName')
It works if I define the specific object (i.e. John.lastName) but not using the array[i], am I doing something wrong?
var person = function(firstName, lastName) {
this.firstName = firstName || "Anonymous";
this.lastName = lastName || "Anonymous";
};
var John = new person("John", "Walter");
var Bob = new person("Bob", "Stevens");
var Gerry = new person("Gerry", "Cricket");
var Frank = new person("Frank", "Bloom");
var people = [John, Bob, Gerry, Frank];
for (i = 0; i < people.length; i++) {
$(document).ready(function() {
$("body").append("<table><tr><td>" + people[i].lastName + "</td></tr></table>");
});
};
$(document).ready(function() {inside of the foreach loop.