對于我正在制作的應用程式,我想使用復選框從陣列中洗掉專案。這是我使用的功能:
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)時,我得到一個型別錯誤,但我不知道這里出了什么問題。誰能幫我?
先感謝您 :)
uj5u.com熱心網友回復:
發生這種情況是因為在回圈內部您更改了迭代的陣列。
嘗試將所有條件包裝到 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)
}
}
或者您也可以使用“繼續”而不是 if-else 構造
uj5u.com熱心網友回復:
對不起,我沒有足夠的聲譽來回答。你能具體說明錯誤資訊嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/478528.html
標籤:javascript 反应 循环 for循环 拼接
上一篇:c 中的future是否對應于javascript中的promise?
下一篇:根據id合并物件陣列中的物件
