简体   繁体   English

JavaScript-循环数组比我少

[英]JavaScript - Looping array less than I should

I'm trying to loop an array which contains a list of elements returned by ClassName, but I can't loop all of them, because of the next situation: 我正在尝试循环一个包含ClassName返回的元素列表的数组,但是由于下一种情况,我无法循环所有元素:

var list = document.getElementsByClassName('class');

for (var i = 0; i < list.length; i++) {
    var theClass = list[i].className; //once got list[i].
    theClass = theClass.replace('class', '');
    list[i].className = theClass; //twice got list[i].
}

If the size of the list is = 4, I just can loop two times, because I'm getting twice each position per loop. 如果列表的大小= 4,则我只能循环两次,因为每个循环的每个位置都会得到两次。 Do you know what I can do and why it happens? 您知道我能做什么,为什么会发生吗? Thank you. 谢谢。

The data structure returned by getElementsByClassName is Array-like and dynamic based on the DOM. getElementsByClassName返回的数据结构类似于Array,并且基于DOM是动态的。 Once you replace the class on the list item in question, you end up losing an item per iteration. 一旦替换了有问题的列表项上的类,最终每次迭代都会丢失一个项。

To fix this, you can take a copy of the returned values first before operating on them, or work backwards. 要解决此问题,您可以先对返回值进行复制,然后再对它们进行操作,或者向后工作。

Take a copy: 复印:

var list = document.getElementByClassName('class')

var realList = []
Array.prototype.push.apply(realList, list)

for (var i = 0; i < realList.length; i++) {
 // do changes as you have already
}

Working backwards: 向后工作:

var list = document.getElementsByClassName('class')

for (i=list.length - 1; i >= 0; i--) {
   // do changes to list[i]
}

Another poster briefly mentioned a while loop which also works, but then their answer disappeared (I don't want to take credit for this!): 另一位张贴者简短地提到了一个while循环,该循环也有效,但随后他们的答案消失了(我不想为此感到遗憾!):

var list = document.getElementsByClassName('class')

while (list.length != 0) {
    // do changes to list[0]
}

If you write out what happens in your initial code, you can see the problem more clearly: 如果您写出初始代码中发生的情况,则可以更清楚地看到问题:

Iteration 1: i=0, list=[a,b,c,d], length = 4, list[i]=a
Iteration 2: i=1, list=[b,c,d], length = 3, list[i]=c
Before Iteration 3: list=[b,d], i=2, length = 2, loop breaks

Now writing out what happens when using the reverse loop: 现在写出使用反向循环时发生的情况:

Iteration 1: i=3, list=[a,b,c,d], length = 4, list[i]=d
Iteration 2: i=2, list=[a,b,c], length = 3, list[i]=c
Iteration 3: i=1, list=[a,b], length = 2, list[i]=b
Iteration 4: i=0, list=[a], length = 1, list[i]=a

All these solutions are variations on this solution of avoiding using i to reference the middle parts of the array-like result value of getElementsByClassName so that the dynamic nature of it is dealt with. 所有这些解决方案都是该解决方案的变体,避免使用i来引用getElementsByClassName的类似于数组的结果值的中间部分,以便处理其动态性质。

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

相关问题 忽略小于或等于3个javascript数组的单词 - Ignore words less than or equal 3 javascript array 使用JavaScript的网格中的金额不应小于0 - Amount should not be less than 0 in grid using javascript not working 我需要在 javascript 中创建一个函数来显示有多少个数组成员小于给定的数字 - I need to create a function in javascript to show how many members of array are less than a given number Javascript 不循环,因为它应该循环 - Javascript not looping as it should loop JavaScript,如果不起作用 - JavaScript If Less Than Not Working 在 JavaScript 中,我需要从两个对象数组中根据过滤条件过滤掉对象,在一个复杂度小于 O(n^2) 的新数组中 - In JavaScript, from two array of objects I need to filter out objects based on a filter condition, in a new array in complexity less than O(n^2) JavaScript大于或小于 - Javascript Greater than or less than 给定包含数字和字符串的数组,Javascript返回数字小于100 - Javascript return number less than 100 given an array with numbers and strings 如何找到小于Java数组中x的第一个元素? - How to find first element that's less than the x in an array in Javascript? 当我使用变量进行比较时,JavaScript不足以失败 - Javascript less than that fails when I use a variable for comparison
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM