繁体   English   中英

for of循环是否会重新评估过滤后的数组?

[英]Does a `for of` loop re-evaluate a filtered array?

考虑这段JavaScript代码:

cont groups = [{ name: 'Sean', type: 'A' }, 
      { name: 'Jen', type: 'A' }, 
      { name: 'Tom', type: 'B'}]

for(const group of groups.filter(g => g.type === 'A')) {
    ...do some work
}

for循环会在每次迭代时重新评估组过滤器吗? 人们将如何进行测试? 在此示例中,这没什么大不了,但是,在大型集合上,重要的是取而代之将过滤器操作提升到其自己的变量中。

不,它不会在每次迭代时重新评估,而是在开始时进行评估。 For ... of循环与迭代器对象一起使用,该迭代器对象在内部对所有元素索引进行计数。 对于每次迭代,它调用迭代器的next()方法来获取下一个元素。 有关迭代器的更多信息,请参考此链接

基于Pointy在评论中的建议,我将console.log放到了过滤器回调中。 在下面的示例中,我将其更进一步,并使用了一些count变量以确保其不会在每次迭代过程中重新评估filter函数。 如果是的话,我期望值是6(数组中的3个项乘以2个匹配的元素)

 const groups = [{ name: 'Sean', type: 'A' }, { name: 'Jen', type: 'A' }, { name: 'Tom', type: 'B'}] let cnt = 0 for(const group of groups.filter(g => { console.log("I am here") cnt += 1 return g.type === 'A' })) { document.getElementById("count1").value = cnt } cnt = 0 const filteredGroup = groups.filter(g => { console.log("I am here") cnt += 1 return g.type === 'A' }) for(const group of filteredGroup) { document.getElementById("count2").value = cnt } 
 <div> <label>Filter inline with for loop</label> <input type="text" id="count1" /> <br /> <br /> <label>Filter set outside of loop</label> <input type="text" id="count2" /> <br /> </div> 

这似乎可以确认一些注释和RK_15的答案,即它不会重新评估for循环内的过滤器。

暂无
暂无

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

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