JavaScriptQuestion 26 of 36
Task: filtering sports
Answer out loud or in your head first, then compare.
Answer
I return a new array with filter, without changing the input array.
JavaScript
const sports = [
{ name: 'cricket' },
{ name: 'football' },
{ name: 'cricket' },
];
const result = sports.filter(sport => sport.name === 'cricket');
// [{ name: 'cricket' }, { name: 'cricket' }]To be precise: the result holds references to the same objects. filter doesn't make a deep copy. I take the field name and comparison rules from the task statement.
How did it go? Be honest — “Not yet” puts the question on your review list.
Practise in code
- Filtering sports — runs tests in your browser