简体   繁体   English

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

[英]How to skip current iteration of loop if condition is not met?

I want to skip if如果我想跳过

x.configured === true

is false.是假的。 My code(works fine)我的代码(工作正常)

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);

} 

I would like to skip if如果我想跳过

x => x.configured === false

The problem is loop already finished the job.问题是循环已经完成了工作。

How to refactor it?如何重构它?

If you want to skip one iteration use the "continue" keyword or the "break" if you want to stop the loop.如果您想跳过一次迭代,请使用“继续”关键字,如果您想停止循环,请使用“中断”。 Check this page for more information: https://www.w3schools.com/js/js_break.asp查看此页面了解更多信息: https://www.w3schools.com/js/js_break.asp

But in your code you're already filtering "x.configured === true", the "x => x.configured === false" is already skipped但是在您的代码中,您已经在过滤“x.configured === true”,“x => x.configured === false”已经被跳过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM