I have an array of objects defined as such:
const people = [
{
name: "John",
dateOfBirth: 1990,
dateOfDeath: 2020
},
{
name: "Jane",
dateOfBirth: 1970,
dateOfDeath: 2019,
},
{
name: "Micheal",
dateOfBirth: 2002,
}
]
I want to loop through the whole array and set the dateOfDeath for every person still alive to today.
I used
people.forEach ( element => {
if(!("dateOfDeath" in element))
element.dateOfDeath = (new date()).getFullYear();
});
My question is, why does this work? how can the callback arrow function access the array element and modify it? shouldn't it modify only the local element variable (which is the function parameter), hence leaving the array item intact?