在JavaScript中减少由map函数组成的函数

时间:2022-03-16 02:31:31

Say we have

说我们有

var i = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

and want to reduce() it like

并希望减少()它

var plus = function(a, b)
{
  return a + b;
};

var s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  .reduce(plus);

console.log(s);

Now, I want to compose reduce() function itself from map() function.

现在,我想从map()函数中编写reduce()函数。

How would you do that? What is the smartest way?

你会怎么做?什么是最聪明的方式?

thanks!

谢谢!

2 个解决方案

#1


1  

If you're trying to build the map function using reduce, you could do the following (The example I'm providing will use built-in functions and work for arrays only!):

如果您正在尝试使用reduce构建map函数,则可以执行以下操作(我提供的示例将使用内置函数并仅适用于数组!):

var numbers = [1,2,3,4,5];
var map = function(arr, callback) {
  return arr.reduce(function(start, value) {
    start.push(callback(value));
    return start;
  }, []);
};

var newArray = map(numbers, function(value) {
  return value * 3;
});
console.log(newArray); // prints [3,6,9,12,15]

This will iterate through each of the values in our numbers array, invoke (execute) the callback function using the current value we're looping over, and then push this value to an empty array which will be returned at the end of reduce. In other words, it will map the results of our callback function to a new array!

这将迭代遍历数字数组中的每个值,使用我们循环的当前值调用(执行)回调函数,然后将此值推送到将在reduce结束时返回的空数组。换句话说,它会将我们的回调函数的结果映射到一个新数组!

That being said, if you're interested in functional programming, I would encourage you to check out underscorejs's annotated source code.

话虽如此,如果您对函数式编程感兴趣,我建议您查看下划线的带注释的源代码。

#2


2  

map returns one value for every value in the array, thus the result will be an array just as big as the input array. The only way to get one value out of it is by sending only one value into it.

map为数组中的每个值返回一个值,因此结果将是一个与输入数组一样大的数组。从中获取一个值的唯一方法是只向其中发送一个值。

[[1,2,3,4,54]].map(
    function(d){
        sum = d.reduce(function(s,d){
                     return s+d
                 });
        console.log(sum)
    }
);

#1


1  

If you're trying to build the map function using reduce, you could do the following (The example I'm providing will use built-in functions and work for arrays only!):

如果您正在尝试使用reduce构建map函数,则可以执行以下操作(我提供的示例将使用内置函数并仅适用于数组!):

var numbers = [1,2,3,4,5];
var map = function(arr, callback) {
  return arr.reduce(function(start, value) {
    start.push(callback(value));
    return start;
  }, []);
};

var newArray = map(numbers, function(value) {
  return value * 3;
});
console.log(newArray); // prints [3,6,9,12,15]

This will iterate through each of the values in our numbers array, invoke (execute) the callback function using the current value we're looping over, and then push this value to an empty array which will be returned at the end of reduce. In other words, it will map the results of our callback function to a new array!

这将迭代遍历数字数组中的每个值,使用我们循环的当前值调用(执行)回调函数,然后将此值推送到将在reduce结束时返回的空数组。换句话说,它会将我们的回调函数的结果映射到一个新数组!

That being said, if you're interested in functional programming, I would encourage you to check out underscorejs's annotated source code.

话虽如此,如果您对函数式编程感兴趣,我建议您查看下划线的带注释的源代码。

#2


2  

map returns one value for every value in the array, thus the result will be an array just as big as the input array. The only way to get one value out of it is by sending only one value into it.

map为数组中的每个值返回一个值,因此结果将是一个与输入数组一样大的数组。从中获取一个值的唯一方法是只向其中发送一个值。

[[1,2,3,4,54]].map(
    function(d){
        sum = d.reduce(function(s,d){
                     return s+d
                 });
        console.log(sum)
    }
);