.map,.every和.forEach有什么区别?

编程入门 行业动态 更新时间:2024-10-16 00:24:17
本文介绍了.map,.every和.forEach有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我总是想知道他们之间的区别是什么。他们似乎都做同样的事情...

解决方案

区别在于返回值。 $ b

.map () 通过对原始项目执行一些操作而创建一个新的对象数组。

.every() 返回布尔值 - 如果此数组中的每个元素均满足提供的测试函数,则为true。与 .every()的一个重要的不同之处在于可能不总是为数组中的每个元素调用测试函数。一旦测试函数对任何元素返回false,不会再有数组元素被迭代。因此,测试功能通常不应该有副作用。

.forEach() 不返回任何内容 - 迭代Array对Array中的每个项目执行给定的操作。编辑:以下是 MSDN文档如果你愿意。

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

解决方案

The difference is in the return values.

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

.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() returns nothing - It iterates the Array performing a given action for each item in the Array.

Edit: Here's the MSDN Docs if you prefer.

更多推荐

.map,.every和.forEach有什么区别?

本文发布于:2023-11-25 07:46:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   map   forEach

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!