user2950593
user2950593

Reputation: 9627

Map array js - how to pass argument

If I write map like this:

days=['m', 't'];
days.map(paste(day));

function paste(day) {
  alert(day)
}

It doesn't work;

How can i pass my argument day to function paste?

Upvotes: 3

Views: 2104

Answers (2)

Royi Namir
Royi Namir

Reputation: 148524

days=['m', 't'];
days.map(function (a){paste(a)});

function paste(day) {
      alert(day)
}

This works but aga's is better. ( shorter).

However - notice cross platform issue http://jsbin.com/axaluq/42?q=array%20map

Upvotes: 0

aga
aga

Reputation: 29416

You need to pass your paste function to map, not invoke it:

var days = ['m', 't'];
days.map(paste);

function paste(day) {
  alert(day)
}

map function will iterate through the days array and invoke the function you passed in it on every object of the days.

Upvotes: 4

Related Questions