Question: Why don’t the names show up in output of this code?
Repl link:
const actors = [
{name: 'Mr Green', netWorth: 2000000},
{name: 'Lupita', netWorth: 10},
{name: 'Jumbo', netWorth: 10000000},
]
console.log(actors.filter(actor => actor.netWorth > 10))
1 Like
Just loop over the filtered array and display each name.
Change this:
console.log(actors.filter(actor => actor.netWorth > 10))
To this:
const filteredActors = actors.filter(actor => actor.netWorth > 10);
filteredActors.forEach(actor => console.log(actor.name));
1 Like
Your questions is in javascript
not java
3 Likes
Reason why names wont show up is that way filter is used in that case it returns array of objects.
This is what it returns.
[{name: 'Mr Green', netWorth: 2000000},
{name: 'Jumbo', netWorth: 10000000}]
To print out just names you need to have extra step.
const actors = [
{ name: 'Mr Green', netWorth: 2000000 },
{ name: 'Lupita', netWorth: 10 },
{ name: 'Jumbo', netWorth: 10000000 },
];
const filteredActors = actors.filter(actor => actor.netWorth > 10);
for (const { name } of filteredActors) {
console.log(name);
}
/*
output:
Mr Green
Jumbo
*/
1 Like
Or, for a one liner:
console.log(actors.filter(actor => actor.netWorth > 10).map(a=>a.name));