admin 管理员组

文章数量: 1086019

In EE (Google Earth Engine Javascript API) I can do

var listOfNumbers = [0, 1, 1, 2, 3, 5];
print('List of numbers:', listOfNumbers);

var add_ten = function(n) {
  var m = n + 10;
  return m;
}

var listOfNumbers_ = listOfNumbers.map(add_ten);
print('List of numbers:', listOfNumbers_);

What if I want to add x (or another value) instead of 10? Like

var listOfNumbers = [0, 1, 1, 2, 3, 5];
print('List of numbers:', listOfNumbers);

var add_x = function(n, x) {
  var m = n + x;
  return m;
}

var listOfNumbers_ = listOfNumbers.map(add_x);
print('List of numbers:', listOfNumbers_);

How do I pass that x?

I tried

var listOfNumbers_ = listOfNumbers.map(add_x(100));
print('List of numbers:', listOfNumbers_);

But got NaN is not a function.

Also tried

var listOfNumbers_ = listOfNumbers.map(add_x, 100);
print('List of numbers:', listOfNumbers_);

Then got the following interesting result (which I don't understand)

0,2,3,5,7,10

In EE (Google Earth Engine Javascript API) I can do

var listOfNumbers = [0, 1, 1, 2, 3, 5];
print('List of numbers:', listOfNumbers);

var add_ten = function(n) {
  var m = n + 10;
  return m;
}

var listOfNumbers_ = listOfNumbers.map(add_ten);
print('List of numbers:', listOfNumbers_);

What if I want to add x (or another value) instead of 10? Like

var listOfNumbers = [0, 1, 1, 2, 3, 5];
print('List of numbers:', listOfNumbers);

var add_x = function(n, x) {
  var m = n + x;
  return m;
}

var listOfNumbers_ = listOfNumbers.map(add_x);
print('List of numbers:', listOfNumbers_);

How do I pass that x?

I tried

var listOfNumbers_ = listOfNumbers.map(add_x(100));
print('List of numbers:', listOfNumbers_);

But got NaN is not a function.

Also tried

var listOfNumbers_ = listOfNumbers.map(add_x, 100);
print('List of numbers:', listOfNumbers_);

Then got the following interesting result (which I don't understand)

0,2,3,5,7,10
Share Improve this question edited May 18, 2019 at 4:38 KcFnMi asked May 18, 2019 at 4:34 KcFnMiKcFnMi 6,20916 gold badges80 silver badges174 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

If you don't want to change your current function, then you can use partial application via Function#bind to supply one argument to it but not the other:

var listOfNumbers = [0, 1, 1, 2, 3, 5];
var add_x = function(n, x) {
  var m = n + x;
  return m;
}

var add_10 = add_x.bind(null, 10);

var listOfNumbers_ = listOfNumbers.map(add_10);

console.log(listOfNumbers_);

Or even even:

var listOfNumbers_ = listOfNumbers.map(add_x.bind(null, 10));

Alternatively, you can use currying:

var listOfNumbers = [0, 1, 1, 2, 3, 5];
var add_x = function(n) {
  return function (x){
    var m = n + x;
    return m;
  }
}

var add_10 = add_x(10);

var listOfNumbers_ = listOfNumbers.map(add_10);

console.log(listOfNumbers_);

Or even:

var listOfNumbers_ = listOfNumbers.map(add_x(10));

You can shorten the curried definition using arrow functions:

var add_x = n => x => n+x;

The array sum makes perfect sense when you look at the function signature of Array#map, as the first two parameters provided to the callback function are the element and the index iterated.

You have at least two choices:

  1. Function#bind, to prefix the initial argument n and let x be the element from the array,
  2. Anonymous function expression:

E.g.

const arr = [0, 1, 1, 2, 3, 5];
const r1 = arr.map(add_x.bind(null, n));
const r2 = arr.map(x => add_x(n, x));

Here is a method using currying:

var listOfNumbers = [0, 1, 1, 2, 3, 5];
console.log('List of numbers:', listOfNumbers);

var add_x = (n) => (x) => {
  return n + x;
}

var listOfNumbers_ = listOfNumbers.map(add_x(100));
console.log('List of numbers:', listOfNumbers_);

Note the add_x variable is set to a function that is called with the second value as the parameter.

When used with the .map() method, the first value es from the array and the second is provided as the parameter to the function. It is the equivalent of calling it like this:

add_x(1)(100)

You can simply write a new function and pass it to the .map() method:

function mapFn(value) { return add_x(value, 100) }
listOfNumbers.map(mapFn)

You can use arrow function syntax to make it even simpler:

listOfNumbers.map(value => add_x(value, 100))

本文标签: javascriptCan I map a function with more then one argumentStack Overflow