I think this is fairly straightforward but I am not sure. I am trying to insert copies of an object into an array, changing the value of some of them. Ideally I would like to do this without a constructor function. Here is what I have:
var person = {
gender: 'Male',
age: 50
}
var people = [];
for (var i = 0; i < 5; i++) {
var favoriteWord = "hi";
person.favoriteWord = favoriteWord;
people.push(person);
}
for (var i = 0; i < 5; i++) {
var favoriteWord = "goodbye";
person.favoriteWord = favoriteWord;
people.push(person);
}
console.log(people)
What happens is that every item in the array takes on the same favoriteWord
property. In essence all of the items in the array are the same.
Is there a way to change this or do I have to use a function?