I'm having a bit of trouble figuring out why the persons
array contains the new age
key:value pair.
I thought by slicing it (and therefore creating an independent copy) I could work solely on the newArray
and only append age
to it ... but it seems to be updating persons
as well??
Thanks in advance
const persons = [
{ first: 'Albert', last: 'Einstein', birthYear: 1879, deathYear: 1955 },
{ first: 'Isaac', last: 'Newton', birthYear: 1643, deathYear: 1727 },
{ first: 'Galileo', last: 'Galilei', birthYear: 1564, deathYear: 1642 },
];
function appendAge(originalArray) {
var newArray = originalArray.slice(0);
newArray.forEach(item => {
item['age'] = item.deathYear - item.birthYear;
})
return newArray
}
console.info(persons)
var newPersons = appendAge(persons)
console.info(newPersons)