lodash中transform和reduce之间的区别是什么 [英] What's the difference between transform and reduce in lodash

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

问题描述

除了声明转换是一种更有效的替代方法之外,我找不到差异的文档。 lodash中转换和减少之间有什么区别(除了慢25%之外)?

Other than stating "transform is a more powerful alternative to reduce", I can find no documentation of what the differences are. What are the differences between transform and reduce in lodash (Other than it being 25% slower)?

推荐答案

在我拉入实用程序之前,我想深入研究源代码。对于lo-dash而言,这可能很困难,因为所有实用程序中都有大量抽象的内部功能。

I like to dive into the source code before I pull in utilities. For lo-dash this can be difficult as there is a ton of abstracted internal functionality in all the utilities.

  • transform source
  • reduce source

所以明显的区别是:


  • 如果你没有指定累加器(通常称为备忘录
    如果您习惯用下划线表示, _。transform 将猜测您是否需要
    一个数组或对象,而reduce将使累加器成为集合的初始项。

  • If you dont specify the accumulator (commonly referred to as memo if you're used to underscore), _.transform will guess if you want an array or object while reduce will make the accumulator the initial item of the collection.

示例,数组或对象地图通过转换

Example, array or object map via transform:

_.transform([1, 2, 3], function(memo, val, idx) {
    memo[idx] = val + 5;
});
// => [6, 7, 8]

与减少相比(注意,您必须知道输入类型!)

Versus reduce (note, you have to know the input type!)

_.reduce([1, 2, 3], function(memo, val, idx) {
    memo[idx] = val + 5;
    return memo;
}, []);

因此,使用reduce时,下面将计算数组的总和,这是不可能的转换为 a 将是一个数组。

So while with reduce the below will compute the sum of an array, this wouldn't be possible with transform as a will be an array.

var sum = _.reduce([1, 2, 3, 4], function(a, b) {
    return a + b;
});




  • 另一个很大的区别是你不必返回累加器 transform 并且累加器不能更改值(即它将始终是相同的数组)。我说这是函数的最大优点,因为我经常忘记使用reduce返回累加器。

    • Another big difference is you don't have to return the accumulator with transform and the accumulator can't change value (ie it will always be the same array). I'd say this is the biggest advantage of the function as I've often forgotten to return the accumulator using reduce.
    • 例如如果你想将数组转换为带有reduce的值字典,你可以编写如下代码:

      For example if you wanted to convert an array to dictionary of values with reduce you would write code like the following:

      _.reduce([1, 2, 3, 4, 5], function(memo, idx) {
         memo[idx] = true;
         return memo;
      }, {});
      

      我们可以很好地编写这个,而不需要像reduce中那样返回累加器

      Whereas with transform we can write this very nicely without needing to return the accumulator like in reduce

      _.transform([1, 2, 3, 4, 5], function(memo, idx) {
         memo[idx] = true;
      }, {});
      




      • 另一个区别是我们可以通过返回false退出转换迭代。 / li>

        • Another distinction is we can exit the transform iteration by returning false.
        • 总的来说,我认为reduce是一种更灵活的方法,因为你可以更好地控制累加器,但转换可以用来以更简单的方式为某些情况编写等效代码。

          Overall, I'd say reduce is a more flexible method, as you have more control over the accumulator, but transform can be used to write equivalent code for some cases in a simpler style.

          这篇关于lodash中transform和reduce之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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