83

I know that I can use map with a function of one variable in the following manner:

var squarefunc = function(x) {
    return x*x;
};
values = [1,2,3,4]
values.map(squarefunc) // returns [1,4,9,16]

How do I use map with the following function:

var squarefuncwithadjustment = function(x, adjustment) {
    return (x*x + adjustment);
}

where, I want to input value for argument adjustment manually when I call map, say adjustment=2, and have the value of x taken from the array values.

0

8 Answers 8

120

Use an anonymous function:

values.map(
  function(x) { return squarefuncwithadjustment(x, 2); }
);
Sign up to request clarification or add additional context in comments.

1 Comment

this initiates the promise rather than saving it for the Promise.all(). How can I set and consume the promise declaratively?
56

You could use a callback creation function:

var createSquareFuncWithAdjustment = function(adjustment) {
    return function(x) { return (x * x) + adjustment; };
};

values = [1, 2, 3, 4];
values.map(createSquareFuncWithAdjustment(2)); // returns [3, 6, 11, 18]

4 Comments

This is the solution that solves the wider problem of passing ancilliary parameters to a map-reduce chain, so gets my vote.
@Sridhar-Sarnobat is this an example of a closure ?
@Courtney I’ve never fully figured out what a closure is, but I would guess the answer is yes.
@Courtney every function in JavaScript will close over the scope in which it was defined. In this specific case the inner anonymous function preserves access to the adjustment argument.
40

As of ES6 you can use:

.map(element => fn(element, params))

In your case if I want to use 3 as adjustment:

values = [1,2,3,4]
values.map(n => squarefuncwithadjustment(n, 3))

Comments

10

If you reverse the order of your arguments, you can bind the adjustment as the first argument, so that the x will be passed as the second.

var squarefuncwithadjustment = function(adjustment, x) {
    return (x*x + adjustment);
}

values.map(squarefuncwithadjustment.bind(null, 2)); // [3, 6, 11, 18]

The first argument to .bind sets the calling context, which doesn't matter here, so I used null. The second argument to .bind binds 2 as the first argument when invoked.

It may be better to store the function as a bound version.

var squareFuncWith2 = squarefuncwithadjustment.bind(null, 2);

Then use it with .map.

values.map(squareFuncWith2); // [3, 6, 11, 18]

Comments

5

Well!! You can easily pass a second parameter to the map function. The following method is widely used to pass this parameter which generally gets hidden during the call:

values.map(function(x , this) {
    return x*x + this.adjustment;
});

var adjustment = 1;
var values = [1,2,3,4]
values.map(function(x , adjustment) {
    return x*x + adjustment;
});

OR

var adjustment = 1;
var squarefunc = function(x , adjustment) {
    return x*x + adjustment;
};
values = [1,2,3,4]
values.map(squarefunc);

Comments

1

To perform this within a single function, you can add a dash of IIFE to your Curry.

function mapSingleFunc(values, adjustment) {
  return values.map((adjustment => x => (x * x) + adjustment)(adjustment));
};
console.log(mapSingleFunc([1,2,3,4], 2))

In the most abstract sense you are able to tunnel your values in through the calling array. Adding an IIFE allows you to feed the adjustment in each time in the closure.

Comments

1

ES6+ :

values.map( x => squarefuncwithadjustment(x,2) );

Comments

-2

var squarefuncwithadjustment = (x, adjustment) => { return (x*x + adjustment); }

Then

values = values.map( x => squarefuncwithadjustment(x, 2) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.