Using javascript map with a function that has two arguments

Javascript

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

Use an anonymous function:

values.map(
  function(x) { return squarefuncwithadjustment(x, 2); }
);

Solution 2 - Javascript

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]

Solution 3 - Javascript

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))

Solution 4 - Javascript

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]

Solution 5 - Javascript

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);

Solution 6 - Javascript

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.

Solution 7 - Javascript

ES6+ :

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

Solution 8 - Javascript

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

Then

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCurious2learnView Question on Stackoverflow
Solution 1 - JavascriptcasablancaView Answer on Stackoverflow
Solution 2 - JavascriptPeterView Answer on Stackoverflow
Solution 3 - JavascriptDima VinogradovView Answer on Stackoverflow
Solution 4 - Javascriptgray state is comingView Answer on Stackoverflow
Solution 5 - JavascriptajaykumarView Answer on Stackoverflow
Solution 6 - JavascriptleerssejView Answer on Stackoverflow
Solution 7 - JavascriptMCFreddie777View Answer on Stackoverflow
Solution 8 - JavascriptMenziesView Answer on Stackoverflow