为了账号安全,请及时绑定邮箱和手机立即绑定

由于某种原因,使用过滤器过滤对象数组不起作用

由于某种原因,使用过滤器过滤对象数组不起作用

慕仙森 2021-10-29 14:56:22
大家好,我制作了一个 todo 应用程序,它接受用户的输入并使它们成为 todos,我还添加了一个过滤框,但由于某种原因它似乎不起作用(它曾经在将代码拆分为函数调用之前起作用)和基本代码,所以我要把整个代码放在这里,我希望你理解代码。编辑:过滤返回包含过滤输入框中文本的待办事项。函数调用部分://Read and parse existing data in local storageconst getSavedTodos = function() {    const todoJSON = localStorage.getItem('todo');    if (todoJSON !== null) {    return JSON.parse(todoJSON)    } else {        return []    }}// save the todos in locale storageconst saveTodos = function(todos) {    localStorage.setItem('todo', JSON.stringify(todos))}// Render todos let renderTodos = function(todo, filters) {    let filteredTodo = todo.filter(function(todo){        return todo.text.toLowerCase().includes(filters.text.toLowerCase());    })    filteredTodo = todos.filter(function(todo){        return !filters.hideCompleted || !todo.completed;    })    document.querySelector('#todo').innerHTML = '';    const incompleteTodos = filteredTodo.filter(function (todos) {        return !todos.completed    })    const summary = document.createElement('h2')    summary.textContent = `You have ${incompleteTodos.length} todos left`    document.querySelector('#todo').appendChild(summary)    filteredTodo.forEach(function(todo){        let p = document.createElement('p');        p.textContent = todo.text;        document.querySelector('#todo').appendChild(p);    })}基本代码部分:let todos = getSavedTodos();let filters = {    text: '',    hideCompleted: false}renderTodos(todos, filters);document.querySelector('#filter').addEventListener('input', function(e){    filters.text = e.target.value.toLowerCase();    renderTodos(todos, filters);})
查看完整描述

1 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

改变


filteredTodo = todos.filter(function(todo){

    return !filters.hideCompleted || !todo.completed;

})


filteredTodo = filteredTodo.filter(function(todo){

    return !filters.hideCompleted || !todo.completed;

})

否则,您将废弃原始过滤器。


查看完整回答
反对 回复 2021-10-29
  • 1 回答
  • 0 关注
  • 167 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信