代码之家  ›  专栏  ›  技术社区  ›  MikiBelavista

如果条件不满足,如何跳过循环的当前迭代?

  •  0
  • MikiBelavista  · 技术社区  · 3 年前

    如果

    x.configured === true
    

    是错误的。 我的代码(运行良好)

    for (let i = 0; i < appids.length; i++) {
        let appid = appids[i];
        const endpointurl = baseurl + appid + '/branches'
        setTimeout(() => {
            fetch(endpointurl, {
                headers: { Authorization: token }
            }).then(response => response.json())
                .then(data => {
                    if (Array.isArray(data)) {
                        console.log('appid ' + appid + ' has ' + data.filter(x => x.configured === true).length + ' configured branches');
                    }
                });
        }, i * 1000);
    
    } 
    

    如果

    x => x.configured === false
    

    问题是循环已经完成了工作。

    如何重构它?

    0 回复  |  直到 3 年前
        1
  •  1
  •   ragnar    3 年前

    如果要跳过一次迭代,请使用“continue”关键字,如果要停止循环,则使用“break”。有关详细信息,请查看此页面: https://www.w3schools.com/js/js_break.asp

    但在您的代码中,您已经在筛选“x.configured===true”,“x=>x.configured==false”已经被跳过