用于循环跳过数组中的项的JavaScript

用于循环跳过数组中的项的JavaScript,javascript,for-loop,html-lists,auto-increment,for-in-loop,Javascript,For Loop,Html Lists,Auto Increment,For In Loop,我对这里发生的事情有点困惑。我有一个html无序列表,我要根据属性值将其分为3个单独的列表。这是非常基本的,我知道,但我遇到了一个for循环及其增量的特殊情况 [this is code provided to be worked with] <ul id="queue"> <li want="coffee">Phil(html)</li> <li want="coffee">Sandy(html)</li&

我对这里发生的事情有点困惑。我有一个html无序列表,我要根据属性值将其分为3个单独的列表。这是非常基本的,我知道,但我遇到了一个for循环及其增量的特殊情况

[this is code provided to be worked with]
 <ul id="queue">
        <li want="coffee">Phil(html)</li>
        <li want="coffee">Sandy(html)</li>
        <li want="sandwich">Enrique(html)</li>
        <li want="coffee">Joe(html)</li>
        <li want="muffin">Alex(html)</li>
        <li want="chili">Zoe(html)</li>
        <li want="sandwich">Bahamut(html)</li>
        <li want="timbits">Rydia(html)</li>     
    </ul>
我得到了和它不起作用一样的结果

谁能帮我理解我明显遗漏了什么?
谢谢大家!

您可能已经注意到,将元素移动到文档中的新位置。返回一个类似数组的对象,该对象是活动的,即当您向其“作用域”添加或从中删除元素时,该对象会自动更新


您可以通过每次从
ulList

附加时将
i
递减一来修复循环。主题外:
want
不是有效的HTML属性。考虑使用<代码>数据> 。我认为情况可能是改变了列表中的变量量。使用appendChild()函数时,是否会从ulList中删除元素?尝试console.log()it@bvx89就是这样(+
getElementsByTagName
返回一个实时节点列表/HTMLCollection),为什么不将您的注释转换为答案呢?因为它只是一个理论,而不是一个实例answer@bvx89这是一个答案:)。Swet,好的,因此,因为我正在删除某些内容,所以不添加I++是有效的,因为我的0位置从未改变,但我的列表一直在变小,直到没有列表为止?正确。这就是为什么你的第一个版本有效。iulList[0],但这只能在列表中的每一项都确实被删除的情况下完成。(基本上,这就是您在代码段中所做的工作)。
[this is code provided to be worked with]
<section id="sandwiches">
        <h1>Sandwich line</h1>
        <ul id="sandwich-line">
        </ul>
    </section>


    <section id="coffee">
        <h1>Coffee line</h1>
        <ul id="coffee-line">
        </ul>
    </section>


    <section id="everything-else">
        <h1>Everythin else line</h1>
        <ul id="everything-else-line">
        </ul>
    </section>
        ///////////////////////////////////
        // HTML Queue
        ///////////////////////////////////

        // Grab the Queue element and 
        var ulList = document.getElementById('queue').getElementsByTagName('li');

        for(var i = 0; i < ulList.length;){
            // this finds out what they "want" based on the attribute
            var ulOrder = ulList[i].getAttribute("want");

            // To organize the line we can use if statements
            if (ulOrder === "coffee"){
                coffeeLine.appendChild(ulList[i]);

            } else if (ulOrder === "sandwich"){
                sandLine.appendChild(ulList[i]);
            } else {
                elseLine.appendChild(ulList[i]);
               }
           }
 for (var i = 0; i < ulList.length; i++){
for (var i in ulList) {