I am relatively new to Javascript and I've encountered an issue in my code that I can't quite figure out.
My objective is to build an array of "people" where each person has specific information such as an "id" and a "name". Since I don't know the exact number of "people" ahead of time, I'm using the "push" method whenever I need to add another individual. The problem arises when all the entries in my array end up reflecting the data of the latest person added.
Below are the declarations I'm working with:
var ppl_arr = [];
var profile = {
id: 10000,
name: "",
};
profile.id = 3;
ppl_arr.push(profile);
alert(ppl_arr[0].id + "\t" + ppl_arr.length);
profile.id = 5;
ppl_arr.push(profile);
alert(ppl_arr[0].id + "\t" + ppl_arr[1].id + "\t" + ppl_arr.length);
The first alert functions correctly: "3 1"
However, the second alert outputs: "5 5 2" instead of the expected "3 5 2"
Essentially, I'm successfully adding two individuals to my array, but it seems like the data for the second person overrides the first one. Can anyone shed some light on what's happening here?