.map、.every 和 .forEach 之间有什么区别? [英] What is the difference between .map, .every, and .forEach?

查看:25
本文介绍了.map、.every 和 .forEach 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道它们之间的区别是什么.他们似乎都在做同样的事情......

I've always wondered what the difference between them were. They all seem to do the same thing...

推荐答案

区别在于返回值.

.map() 返回一个新的对象数组,这些对象是通过对原始项目采取一些操作而创建的.

.map() returns a new Array of objects created by taking some action on the original item.

.every() 返回一个布尔值 - 如果此数组中的每个元素都满足提供的测试函数,则为真.与 .every() 的一个重要区别是测试函数可能并不总是为数组中的每个元素调用.一旦测试函数对任何元素返回 false,就不再迭代数组元素.因此,测试函数通常应该没有副作用.

.every() returns a boolean - true if every element in this array satisfies the provided testing function. An important difference with .every() is that the test function may not always be called for every element in the array. Once the testing function returns false for any element, no more array elements are iterated. Therefore, the testing function should usually have no side effects.

.forEach() 什么都不返回 - 它迭代数组,为数组中的每个项目执行给定的操作.

.forEach() returns nothing - It iterates the Array performing a given action for each item in the Array.

阅读这些以及许多其他ArrayMDN 上的迭代方法.

Read about these and the many other Array iteration methods at MDN.

这篇关于.map、.every 和 .forEach 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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