0

How can i pass parameter to a method which is passed as parameter inside an array. map method.

var result = [1,2,3];
var updatedResult = result.map(modifierMethod);
function modifierMethod(cellValue){ return cellValue* dynamicValue}

I want to pass the dynamicValue into the modifierMethod while calling map method.

starhunter
  • 165
  • 10

2 Answers2

0

You could use bind to add parameters to your function like this

function mapping() { return arguments[0].multy * arguments[1] }

var b = [1,2,3,4,5]
const options = { multy: 5 }
var c = b.map(mapping.bind(null, options))

console.log(c)
damianfabian
  • 1,681
  • 12
  • 16
0

Actiually I do not see any problems ... If I run the following code:

var result = [1,2,3];
var updatedResult = result.map(modifierMethod);
function modifierMethod(cellValue){ return cellValue * 2}
console.log(updatedResult);

It works properly as expected. Output is the following:

[ 2, 4, 6 ]

Maybe I do not understand your issue ?

Denis Kotov
  • 857
  • 2
  • 10
  • 29
  • Well the above definitely works. However I wanted the multiplier to be passed(as dynamic) rather than keep it static(=2, in your case). – starhunter Jan 29 '17 at 07:30