为什么过滤器方法似乎没有像这个TODO应用中的剪接一样工作方式?
我在JS中有一个具有以下功能的todo应用程序:
这是将ID传递到事件侦听器以删除
removeButton.addEventListener('click', function () {
removeTodo(todo.id)
renderTodos(todos, filters)
})
此函数的函数 的一部分删除待办事项并使新的todos罚款 - 我认为我评论的过滤方法也可以使用,但它确实删除了todo,但是除非我刷新页面,否则它不会自动更新浏览器中的列表,虽然剪接是自动的,但为什么会发生这种情况?可以在RenderTodos开始阅读列表之前等待更新本地存储吗?只需注意,在不起作用的示例中,我将newtodos传递到了保存功能中,我只是以拼接方式将其更改为Todos。
const removeTodo = function (id) {
const todoIndex = todos.findIndex(function (todo) {
return todo.id === id
})
if (todoIndex > -1) {
todos.splice(todoIndex, 1)
}
// newTodos = todos.filter(function (todo) {
// return todo.id !== id
// })
saveTodos(todos)
}
TODO列表保存在本地存储中
const saveTodos = function (todos) {
localStorage.setItem('todos', JSON.stringify(todos))
}
,这是信息的渲染功能
const renderTodos = function (todos, filters) {
const filteredTodos = todos.filter(function (todo) {
const searchTextMatch = todo.text.toLowerCase().includes(filters.searchText)
const hideCompletedMatch = !filters.hideCompleted || !todo.completed
return searchTextMatch && hideCompletedMatch
})
const todosLeft = filteredTodos.filter(function (todo) {
return !todo.completed
})
document.querySelector('#todos').innerHTML = ''
document.querySelector('#todos').appendChild(generateSummaryDom(todosLeft))
filteredTodos.forEach(function (todo) {
document.querySelector('#todos').appendChild(generateTodoDom(todo))
})
}
I have a todo app in JS with the following functions:
This is part of a function that passes an id into an event listener to remove a todo
removeButton.addEventListener('click', function () {
removeTodo(todo.id)
renderTodos(todos, filters)
})
This function removes the todo - I've used 2 approaches, the findIndex way works great, it removes the todo and renders the new todos fine - I thought the filter approach I've commented would also work but it doesn't, it does remove the todo but it doesn't automatically update the list in the browser unless I refresh the page, while splice does it automatically, why could this happen? could it be waiting to update local storage before renderTodos starts reading the list? Just a note that in the example that didn't work I was passing newTodos into the save function, I just changed it to todos for the splice way.
const removeTodo = function (id) {
const todoIndex = todos.findIndex(function (todo) {
return todo.id === id
})
if (todoIndex > -1) {
todos.splice(todoIndex, 1)
}
// newTodos = todos.filter(function (todo) {
// return todo.id !== id
// })
saveTodos(todos)
}
the todo list is saved in local storage
const saveTodos = function (todos) {
localStorage.setItem('todos', JSON.stringify(todos))
}
Here is the render function for information
const renderTodos = function (todos, filters) {
const filteredTodos = todos.filter(function (todo) {
const searchTextMatch = todo.text.toLowerCase().includes(filters.searchText)
const hideCompletedMatch = !filters.hideCompleted || !todo.completed
return searchTextMatch && hideCompletedMatch
})
const todosLeft = filteredTodos.filter(function (todo) {
return !todo.completed
})
document.querySelector('#todos').innerHTML = ''
document.querySelector('#todos').appendChild(generateSummaryDom(todosLeft))
filteredTodos.forEach(function (todo) {
document.querySelector('#todos').appendChild(generateTodoDom(todo))
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
splice()
突变todos
您然后呈现的数组,而filter()
返回一个不使用的新数组。为了使其与
filter()
一起使用,您需要从删除函数返回newtodos
并渲染返回的数组,而不是原始todos
大批。splice()
mutates thetodos
array which you are then renderering, whilefilter()
returns a new array which you are not making use of.To make it work with
filter()
you will need to return thenewTodos
from the remove function and render the returned array, not the originaltodos
array.重新分配变量没有副作用;重新分配一个标识符对其他地方的标识符没有影响。做
意味着您已经将一些结果放入
newtodos
中,而无需做任何其他事情。它不会放入存储(或渲染,尽管未显示渲染方式)。沿着新的过滤戒酒传递,然后从那里渲染(无论您正在这样做) - 不要忘记声明您的变量。
在直接侦听器回调中删除
RenderTodos
时。Reassigning a variable does not have side-effects; reassigning one identifier has no effect on identifiers elsewhere. Doing
means that you've put some results into
newTodos
without doing anything else with it. It doesn't get put into storage (or rendered, though how you render isn't shown).Pass along the new filtered todos, and render (however you're doing it) from there - and don't forget to declare your variables.
while taking
renderTodos
out of the immediate listener callback.