何时/为什么要使用map/reduce for循环 [英] When/why to use map/reduce over for loops

查看:59
本文介绍了何时/为什么要使用map/reduce for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是第一次使用JavaScript进行对象操作,我有一个问题想知道是否有人可以回答.

So I am getting into a bit of object manipulation in JavaScript for the first time and I have a question I'm wondering if anyone could answer.

当我有一个要操纵的对象时,我可以做一些嵌套的for循环操作,但是JavaScript中内置了一些函数,例如map/reduce/filter和lodash/underscore之类的库.

When I have an object I want to manipulate I could do something to the extent of a few nested for loops, however there are functions built into JavaScript, like map/reduce/filter, and libraries like lodash/underscore.

我认为后者(map/reduce/filter和库)是更好的做法,但是我很好奇为什么.

I assume the latter (map/reduce/filter and the libraries) are better practice but I'm just curious as to why.

我正在做一些相当基本的对象操作,可以通过放置一些适当的for循环来解决,以获取和更改对象中的正确键/值,但是可以使用JS中的函数/库轻松完成.只是好奇它们如何更好-例如更好的性能/更干净的代码/易于使用/还有其他优点.

I am doing some pretty basic object manipulation that could be solved with a few well placed for loops to grab and change the right keys/values in the object, but can be easily done with the functions/libraries in JS. Just curious as to how they are better - like better performance/cleaner code/ease of use/whatever else.

抱歉,没有代码.非常感谢任何人在这里帮助我了解更多信息.

Apologies, there is no code. I would very much appreciate anyone helping me understand more here.

编辑-因此,请参考map()的示例

Edit - so taking from the examples for map()

我可以以javascript.map为例

I could take the example for javascript.map

 var kvArray = [{key:1, value:10}, {key:2, value:20}, {key:3, value: 30}];
var reformattedArray = kvArray.map(function(obj){ 
var rObj = {};
rObj[obj.key] = obj.value;
return rObj;
});

我可以做类似

   var kvArray = [{key:1, value:10}, {key:2, value:20}, {key:3, value: 30}];
var reformattedArray = [];

for(var object in kvArray){
  //combine both values into object inside of kvArray[object]);
 };

更少的代码-但是还有其他值得了解的好处吗?

A lot less code - but any other benefits worth knowing about?

推荐答案

我知道我正在答复旧的答案,但只是想为将来的读者指出.

I know I'm replying to an old answer but just wanted to point out for future readers.

Map缩小和过滤器功能来自函数式编程世界.

Map reduce and filter functions come from the functional programming world.

这些是一流的内置运算符,使用的语言包括Lisp,Haskell和其他语言(ml?).功能语言倾向于在不可变数据上运行运算符,而不是使代码在数据上运行以对其进行操作(例如循环).因此,与提供for和while循环相比,它们提供了更简单但功能强大的界面,例如map,filter和reduce.

These are first class built-in operators in languages like Lisp, Haskell, and others(ml?). Functional languages tend to prefer to run operators over immutable data than make the code run over the data to operate on it (say loops). So they provide simpler but powerful interfaces like map, filter and reduce when compared to providing for and while loops.

它还可以帮助他们满足诸如不变性等其他要求.这就是为什么地图会给您发回新地图而不是对旧地图进行变异的原因.从并发的角度来看,这些功能非常好,尽管它们在某些情况下可能会变慢.

It also helps them satisfy other requirements like immutability etc. That's why maps give u back a new map instead of mutating the old one. These are very good from a concurrency point of view, though they may be slower in certain contexts.

这种方法通常可以减少多线程或高并发应用程序中的代码错误.当多个参与者对同一条数据进行操作时,不变性有助于防止代码踩在彼此的脚趾上.

This approach usually leads to fewer errors in code in multi-threaded or high concurrency apps. When multiple actors act on the same piece of data, immutability helps keep code from stepping on each other's toes.

由于javascript通过提供功能性编程语言的某些功能而试图部分发挥功能,因此在其中实现映射,过滤和简化功能也很有意义.

Since javascript tries to be partially functional by providing some functionalities of functional programming languages, it might have made sense to implement map, filter and reduce functions in it too.

YMMV取决于您使用给定工具的方式.

YMMV depending on what you are doing with the tools you are given.

如果您的代码与for循环配合使用效果更好,请继续努力.

If your code works better with a for loop, go for it.

但是,如果您发现对通用数据使用异步代码,那么最终您会费力地尝试调试循环.打个招呼,以进行映射,缩小和过滤.

But if you ever find asynchronous code munching on common data and you end up splitting your hairs trying to debug a loop. Say to map, reduce and filter.

这篇关于何时/为什么要使用map/reduce for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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