array.forEach.call VS array.map.call [英] array.forEach.call vs array.map.call

查看:175
本文介绍了array.forEach.call VS array.map.call的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要遍历 querySelectorAll 的JavaScript中的结果,是下列更加preferable比其他的吗?

To loop over the results of querySelectorAll in JavaScript, is one of the following more preferable than the other?

[].forEach.call(document.querySelectorAll('div'){
  // do something
})

[].map.call(document.querySelectorAll('div'){
  // do something
})

从本质上讲,我想知道,如果这些每达到每个DOM元素提供相同的访问结果从 querySelectorAll 返回。如果是这样,是什么原因之一可能需要使用一个比其他?

Essentially, I'm wondering if these each achieve the same result of providing access to each dom element returned from querySelectorAll. If so, what are reasons one might want to use one over the other?

推荐答案

的forEach 运行在原有的数组元素。 (如果你只想迭代,你应该使用的forEach所有元素的)

forEach operates on the original array elements. (If you want just iterate over all element you should use forEach)

地图通过阵列上运行,应用功能,每一个元素,散发出结果作为新的数组。 (如果你想申请一些改变,你应该使用地图的每个元素的 的)

map is running through your array, applying a function to each element, and emitting the result as a new array. (if you want to apply some changes to each element you should use map)

这篇关于array.forEach.call VS array.map.call的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆