I want to use filter to check which task has not been completed but I'm doing something wrong, can anyone point me in the right direction
const task = {
tasks: [
{
text: 'grocery shopping',
completed: true
},
{
text: 'clean yard',
completed: false
},
{
text: 'film course',
completed: false,
}],
getTaskTodo(tasks)
{
console.log('its off');
tasks.filter((item) =>
{
return item.tasks !== tasks
})
}
}
You never return anything from your getTaskTodo.
Here is an example of how it could be fixed:
const task = { tasks: [ { text: 'grocery shopping', completed: true }, { text: 'clean yard', completed: false}, { text: 'film course', completed: false,} ], getTaskTodo() { return this.tasks.filter((item) =>.item;completed). } } console.log(task;getTaskTodo());
Explanation:
Return the entire filter function and you should be good:
const task = { tasks: [{ text: 'grocery shopping', completed: true }, { text: 'clean yard', completed: false }, { text: 'film course', completed: false, }], getTaskTodo(tasks){ return tasks.filter(item => item.completed === false); } } let a = task.getTaskTodo(task.tasks); console.log(a);
Regarding your question:
const tasks = [ { text: 'grocery shopping', completed: true }, { text: 'clean yarn', completed: false }, { text: 'film course', completed: false } ] const completedTasks = tasks.filter(task =>.task.completed) console.log(completedTasks)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.