I have this array of objects called movies and I would like to create a new array only with the name property, I've used the forEach method but I need to do it with the filter method:
Expected output with filter method:
[ 'Jumanji', 'Harry P', 'Toy Story' ]
let movies = [
{
'name': 'Jumanji',
'duration': 120
},
{
'name': 'Harry P',
'duration': 60
},
{
'name': 'Toy Story',
'duration': 240
}
];
let movies_names = [];
movies.forEach(movie => {
movies_names.push(movie.name);
});
console.log('movies_names', movies_names)
With filter method I'm able to see the names but when I return them I see the original array.
let movies = [
{
'name': 'Jumanji',
'duration': 120
},
{
'name': 'Harry P',
'duration': 60
},
{
'name': 'Toy Story',
'duration': 240
}
];
let newAr = movies.filter((item, index, arr) => {
console.log('item', item.name)
return item.name
// console.log('index', index)
// console.log('arr', arr.name)
})
console.log('newAr', newAr)
What am I missing?