从循环中的阵列中的剪接项目,获取TypeError
对于一个应用程序,我想使用复选框从数组中删除项目。这是我使用的功能:
for(let article = articleList.length -1; article >= 0; article--)
{
console.log(article, articleList[article].props.title);
if(this.whatToShow.article == false){
if (articleList[article].props.category == "Artikel"){
articleList.splice(article, 1);
}
}
if(this.whatToShow.news == false){
if (articleList[article].props.category == "Nieuws"){
articleList.splice(article, 1);
}
}
if(this.whatToShow.blog == false){
if (articleList[article].props.category == "Blog"){
articleList.splice(article, 1);
}
}
if(this.whatToShow.client == false){
if (articleList[article].props.category == "Client"){
articleList.splice(article, 1);
}
}
if(this.whatToShow.stories == false){
if (articleList[article].props.category == "Ervaringsverhaal"){
articleList.splice(article, 1);
}
}
}
我首先遇到了它不会删除所有项目的问题,因此我向后进行循环。现在,当我几乎将所有复选框关闭(所有值false)时,我会得到一个TypeError,但是我不知道这里出了什么问题。谁能帮我吗?
先感谢您 :)
For an application I am making I want to remove items from an array using checkboxes. This is the function I use:
for(let article = articleList.length -1; article >= 0; article--)
{
console.log(article, articleList[article].props.title);
if(this.whatToShow.article == false){
if (articleList[article].props.category == "Artikel"){
articleList.splice(article, 1);
}
}
if(this.whatToShow.news == false){
if (articleList[article].props.category == "Nieuws"){
articleList.splice(article, 1);
}
}
if(this.whatToShow.blog == false){
if (articleList[article].props.category == "Blog"){
articleList.splice(article, 1);
}
}
if(this.whatToShow.client == false){
if (articleList[article].props.category == "Client"){
articleList.splice(article, 1);
}
}
if(this.whatToShow.stories == false){
if (articleList[article].props.category == "Ervaringsverhaal"){
articleList.splice(article, 1);
}
}
}
I first had the problem that it wouldn't remove all the items, so I made the loop backwards. Now when I have almost all checkboxes off (all values false) I get a typeerror, but I have no idea what's going wrong here. Can anyone help me?
Thank you in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
之所以发生,是因为在循环内部,您会更改迭代的数组。
尝试将所有条件包装到这样的IF-ELSE:
或者您也可以使用“继续”而不是else struction
It happens because inside the loop you change the array that you iteration over.
Try to wrap all conditions to if-else like this:
Or also you can use "continue" instead if-else construction