Write a function that filters out all fields except 'firstName' and 'lastName' from the objects.
Check out this code snippet I came up with. Any feedback?
let people = [
{
firstName: 'John',
lastName: 'Clark',
gender: 'male'
},
{
firstName: 'Kaily',
lastName: 'Berserk',
gender: 'female'
},
{
firstName: 'Steven',
lastName: 'Bergeron',
gender: 'male'
}
];
function filterNamesOnly(arr) {
let first = 'firstName';
let last = 'lastName';
return arr.forEach(person => {
for (const key in person) {
if (key !== first && key !== last) {
delete person[key];
}
}
})
}
console.log(filterNamesOnly(people));
console.log(people);