区别在于返回值。 $ 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有什么区别?
发布评论