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

查看:143
本文介绍了.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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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