Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS循环-如何知道循环的所有附加条件是否满足_Javascript_Loops - Fatal编程技术网

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

Javascript JS循环-如何知道循环的所有附加条件是否满足,javascript,loops,Javascript,Loops,注释在代码中(我只是不知道如何检查循环中的所有项是否满足条件,如果满足,则执行操作): //编辑-更真实的例子 因为工作示例非常复杂,让我们先。。。测试页面链接(或页面上的任何html元素) HTML: 链接测试 JS: let links=document.getElementsByTagName('a'); 让linksLength=links.length; 设titleCount=0; 对于(i=0;i

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

//编辑-更真实的例子

因为工作示例非常复杂,让我们先。。。测试页面链接(或页面上的任何html元素)

HTML:


链接测试
JS:

let links=document.getElementsByTagName('a');
让linksLength=links.length;
设titleCount=0;
对于(i=0;i
我建议使用
过滤器
方法,然后使用结果获得计数和最后一个元素

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

relevantData = data.filter(e => [4,5,6].includes(e));
count = relevantData.length;
lastItem = relevantData.slice(-1)
let links=document.getElementsByTagName('a');
console.log('============================');
log('links HTML集合:\n');
控制台日志(链接);
console.log('============================');
让linksLength=links.length;
设linkstitelcount=0;
让html='';
让linksArray=Array.from(links);//将HTML集合转换为数组
让AllLinksThatDoeSwaveTitleAttr=linksArray.filter(函数(l)
{
return(l.getAttribute('title')!==undefined和&l.getAttribute('title')!==null和&l.getAttribute('title')!='';
});
让AllLinkSthatHatShaveTitleAttrCount=AllLinkSthatHatShaveTitleAttr.length;
console.log();
console.log('============================');
log('alllinksthatdeshavetitleAttribute:\n');
console.log(所有链接都不包含titleattrcount);
console.log('============================');
//设lastItem=relevantData.slice(-1)
对于(i=0;i
=
=
之间有很大区别,数据真的只是一个数字吗?或者你打算使用数组?@alon right-==:)(2)henry我计划在这里使用大量的东西(主要是大数字)。我想看看你的问题的更现实的例子会有所帮助。从这个例子来看,循环似乎有些过分。如果
数据
只是一个数字,而您的条件是小于
数据
的数字等于4、5或6的数量,那么您可以使用
If、else If、else
@Henry添加了更现实的示例来实现这一点。
    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)
        }
    }
relevantData = data.filter(e => [4,5,6].includes(e));
count = relevantData.length;
lastItem = relevantData.slice(-1)
                        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);