0
function commonFunction(x, y) {
  return x * y * 2;
}

var ints = [1,2,3];
var result = ints.map(commonFunction);
//result = [0,4,12]

Correct me if i'm wrong, commonFunction is expecting 2 parameters and by calling commonFunction inside a Array.map, first parameter automatically filled up by the each individual of the array.

As shown above, second parameter wasn't provided, why it yields the result of [0,4,12]?

I understand that in order to achieve the correct result, I can use bind as below:

var result = ints.map(commonFunction.bind(this,3));
//result = [6,12,18]
Isaac
  • 12,042
  • 16
  • 52
  • 116
  • Second parameter is the index. – Satish Kumar May 19 '18 at 17:31
  • This question is *not* a duplicate of the one linked, that question is about parseInt and the answer was in the radix param, this one is about map and its about the index param. – Josu Goñi May 19 '18 at 17:39
  • @JotaGe: Oh goodness, yes it's a duplicate. Do you really think we need a new question for every possible function when the root cause is exactly the same? –  May 19 '18 at 17:54
  • No, the first parameter is not filled by the array member. That's not how `.bind()` works. And having a `z` parameter defined doesn't alter the behavior of your last example. –  May 19 '18 at 17:57
  • 1
    Calling a bound function *appends* the arguments: `sth.bind(context, one, two)(three, four)` is the same as `sth(one, two, three, four)` – Jonas Wilms May 19 '18 at 17:58

2 Answers2

1

Map calls your commonFunction with 3 parameters, the seccond one is the index.

var new_array = arr.map(function callback(currentValue[, index[, array]]) {
    // Return element for new_array
}[, thisArg])

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

That is why you are getting [1*0*2, 2*1*2, 3*2*2]

About binding:

After commonFunction.bind(this,3) you get a function that always get a x=3, but that is really weird, you should just do the following:

map(value => commonFunction(3, value))

Community
  • 1
  • 1
Josu Goñi
  • 1,178
  • 1
  • 10
  • 26
  • Hi JotaGe, could you please try to have a look at my updated question? – Isaac May 19 '18 at 17:53
  • You should ask another question, specially having in mind that this one is already marked both as duplicated and as answered. – Josu Goñi May 19 '18 at 18:01
  • I have updated my answer with a better way to solve your problem. And please do not change your question on the way. – Josu Goñi May 19 '18 at 18:10
1

The second parameter for a .map() callback is the array index, so you're winding up getting that as your 'y' value each time. (Just for completeness, though it's not directly relevant to the question, the third parameter is the full array:)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

function commonFunction(x, y, z) {
  console.log("X: ",x); // the array value
  console.log("Y: ",y); // the array index
  console.log("Z: ",z); // the full array
  return x * y * 2;
}

var ints = [1,2,3];
var result = ints.map(commonFunction);
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
  • Hi Daniel, could you please try to have a look at my updated question? – Isaac May 19 '18 at 17:53
  • Hi, @Isaac -- It's best not to rewrite questions to a substantially different topic, as that can make the Q&A less useful for future users (because it's not always clear which version of the question each answer applies to.) You may want to post that as its own separate question, unless the comments you've received above are sufficient answer – Daniel Beck May 19 '18 at 18:01