从循环中的阵列中的剪接项目,获取TypeError

发布于 2025-01-31 05:25:25 字数 1108 浏览 4 评论 0原文

对于一个应用程序,我想使用复选框从数组中删除项目。这是我使用的功能:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2025-02-07 05:25:25

之所以发生,是因为在循环内部,您会更改迭代的数组。

尝试将所有条件包装到这样的IF-ELSE:

  const articleList = [
    { props: { title: 'first', category: 'Artikel' } },
    { props: { title: 'sec', category: 'Blog' } },
    { props: { title: 'third', category: 'Client' } },
    { props: { title: 'fouth', category: 'Nieuws' } },
  ]

  for (let article = articleList.length - 1; article >= 0; article--) {
    console.log(article, articleList[article].props)
    if (this.whatToShow.article == fals && articleList[article].props.category == 'Artikel') {
      articleList.splice(article, 1)
    } else if (this.whatToShow.news == false && articleList[article].props.category == 'Nieuws') {
      articleList.splice(article, 1)
    } else if (this.whatToShow.blog == false && articleList[article].props.category == 'Blog') {
      articleList.splice(article, 1)
    } else if (this.whatToShow.client == false && articleList[article].props.category == 'Client') {
      articleList.splice(article, 1)
    }
  }

或者您也可以使用“继续”而不是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:

  const articleList = [
    { props: { title: 'first', category: 'Artikel' } },
    { props: { title: 'sec', category: 'Blog' } },
    { props: { title: 'third', category: 'Client' } },
    { props: { title: 'fouth', category: 'Nieuws' } },
  ]

  for (let article = articleList.length - 1; article >= 0; article--) {
    console.log(article, articleList[article].props)
    if (this.whatToShow.article == fals && articleList[article].props.category == 'Artikel') {
      articleList.splice(article, 1)
    } else if (this.whatToShow.news == false && articleList[article].props.category == 'Nieuws') {
      articleList.splice(article, 1)
    } else if (this.whatToShow.blog == false && articleList[article].props.category == 'Blog') {
      articleList.splice(article, 1)
    } else if (this.whatToShow.client == false && articleList[article].props.category == 'Client') {
      articleList.splice(article, 1)
    }
  }

Or also you can use "continue" instead if-else construction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文