Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass Parameter to array.map()?

Tags:

javascript

I am trying to map values to a function that will accept two numbers to multiply but am having trouble doing so (if this doesn't make sense, take a look at the examples below).

I have an array of numbers and I would like to double/triple/quadruple... the values of this array. I have created functions that would do this, and am feeding these double() and triple() into map().

var arr = [1, 2, 3, 4, 5];  function double(num) {   return num * 2; }  function triple(num) {   return num * 3; }  console.log( arr.map(double) ); console.log( arr.map(triple) ); 

This solution is not scalable as what if I want to multiply the values by 5, or 10? I need a more abstract function that would take a parameter of what to multiply. I am confused about how to do this. My attempt so far is:

var arr = [1, 2, 3, 4, 5];  function multiply(num, multiplyBy) {   return num * multiplyBy; }  console.log( arr.map(multiplyBy(4) ); // Uncaught TypeError: NaN is not a function 

How would I pass multiply() the multiplyBy parameter?

like image 855
Jon Avatar asked Aug 29 '17 00:08

Jon


People also ask

How do you pass an array into a map?

To convert an array of objects to a Map , call the map() method on the array and on each iteration return an array containing the key and value. Then pass the array of key-value pairs to the Map() constructor to create the Map object. Copied!

How many parameters does map () function require?

The map function takes two arguments: an iterable and a function , and applies the function to each element of the iterable.

How do I map an array in JavaScript?

The syntax for the map() method is as follows: arr. map(function(element, index, array){ }, this); The callback function() is called on each array element, and the map() method always passes the current element , the index of the current element, and the whole array object to it.

Can we use array in map?

map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.


1 Answers

You can do something called a factory currying. Effectively, you make a function that will return another function which is tailored to your needs. In this case, an anonymous one.

var arr = [1, 2, 3, 4, 5];  function multiplyBy(scale) {     return function(num){         return num * scale;     } }  console.log( arr.map( multiplyBy(4) )); 

This works because the scope of the anonymous function that is returned is within that of the factory outer function. So, whenever we produce a new function, it will retain the value of scale that was given for its production.

Edit: The last part of @Bergi 's answer is the same as mine. The concept is apparently called currying. Thanks @Bergi ! The Factory pattern is more often applied to the production of objects, as Bergi noted, but it was the only thing I could think of at the time, and javascript sort of treats functions like objects. In this specific case, they are effectively similar. Here is a good reference for currying in JavaScript

like image 185
TheCrzyMan Avatar answered Sep 21 '22 13:09

TheCrzyMan