首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JS循环-如何知道循环的所有附加条件是否都满足

JS循环-如何知道循环的所有附加条件是否都满足
EN

Stack Overflow用户
提问于 2018-08-11 06:16:45
回答 2查看 50关注 0票数 -1

注释在代码中(我只是不知道如何检查是否所有项都满足循环中的条件,如果满足,则执行操作):

// EDIT -更多真实的例子。

因为工作示例真的很复杂,让我们假设我们只是...测试页面链接(或页面上的任何html元素)。

HTML:

代码语言:javascript
复制
    <html>
    <head>
    <title>Links test</title>
    </head>
    <body>
<!-- links are random - we do not know how many they are, and how many of them got title attribute -->
        <a href="https://www.google.com" title="google">google</a>
        <a href="https://facebook.com/">facebook<</a>
        <a href="https://www.instagram.com/" title="instagram">instagram</a>
        <a href="https://www.amazon.com/">amazon</a>
        <a href="https://www.apple.com/" title="apple">apple</a>
    </body>
    </html>

JS:

代码语言:javascript
复制
    let links = document.getElementsByTagName('a');
    let linksLength = links.length;
    let titleCount = 0;

    for (i = 0; i < linksLength; i++) {
        if (links[i].getAttribute('title') !== undefined) {
// I need to count all loop part that fulfil the condition
            titleCount += 1;
            // and if it's the last item that fulfil this condition then do something (in this main loop)
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-11 06:26:47

我建议使用filter方法,然后使用结果来获取计数和最后一个元素。

给定一个数字数组data,下面是一个示例:

代码语言:javascript
复制
relevantData = data.filter(e => [4,5,6].includes(e));
count = relevantData.length;
lastItem = relevantData.slice(-1)
票数 0
EN

Stack Overflow用户

发布于 2018-08-12 03:48:24

代码语言:javascript
复制
                        let links = document.getElementsByTagName('a');
                        console.log('======================');
                        console.log('links HTML collection:\n');
                        console.log(links);
                        console.log('======================');
                        let linksLength = links.length;
                        let linksTitleCount = 0;
                        let html = '';

                        let linksArray = Array.from(links); // converting HTML collection to an array

                        let allLinksThatDoesHaveTitleAttr = linksArray.filter(function(l) 
                        {
                            return (l.getAttribute('title') !== undefined && l.getAttribute('title') !== null && l.getAttribute('title') !== '');
                        });
                        let allLinksThatDoesHaveTitleAttrCount = allLinksThatDoesHaveTitleAttr.length;
                        console.log();
                        console.log('======================');
                        console.log('allLinksThatDoesHaveTitleAttribute:\n');
                        console.log(allLinksThatDoesHaveTitleAttrCount);
                        console.log('======================');
                        //let lastItem = relevantData.slice(-1)


                        for (i = 0; i < linksLength; i++) { // main loop throught ALL links
                            // lets supose we... build html element
                            html += 'link' + [i] +'\n';

                            if (links[i].getAttribute('title') !== undefined && links[i].getAttribute('title') !== null && links[i].getAttribute('title') !== '') { // condition that check existance of title attribute - thanks @m_hutley
                                linksTitleCount += 1; // this is cause I need to count all loop part that fulfil the condition
                                console.log(linksTitleCount);
                                console.log();

                                html += 'this link - ' + [i] + ' - does not have title attribute! \n\n';

                                if (linksTitleCount == allLinksThatDoesHaveTitleAttrCount)
                                {
                                    console.log('DONE!'); // we know we are done - we loop throught all elements that does have title attr
                                    // NOW if it's the last item that fulfil this condition then do something (in this main loop) + some async operations that will leater (when data arrive) target specific HTML with specific link (order like in loop - [i])
                                }
                            }
                        }

                        console.log('HTML result is:\n');
                        console.log(html);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51794648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档