Map

Ah, now we get to one of my favorite functions, map. I use map all the freakin' time.

map is pretty simple. Examples work best here:

[1,2,3,4].map((x) => 2*x)

which returns

[ 2, 4, 6, 8 ]

map is a method of all JavaScript arrays. It takes a function, runs each of its elements through that function, and puts the results in a new array, which it returns.

Here's another example:

['pickle', 'banana', 'potato', 'kitty'].map((thing) => `super ${thing}!`)

which returns

[ 'super pickle!',
  'super banana!',
  'super potato!',
  'super kitty!' ]

Ok, let's do a more complicated example, just to show you how useful this function is. Let's assume you've got a list of customers from your database and you need to send them all an email about a special offer at your online store this Sunday.

You've got a function that will send an email, but you need to convert your customer's information to something that function will understand.

The function looks like this:
mail(email_address, subject, body).

Also, you've got to customize the body text of the email to include the customer's name.

Here's what a customer record looks like:

{
    first_name: 'Joe',
    last_name: 'Bob',
    email: 'joe@joebob.org'
}

The subject of the email is "Warp Cores and plasma conduits 70% off this Sunday!"

Body of the email is:
"Hi {customer's first name},

This Sunday only we're offering standard CZ-39 warp engine cores at 30% of their normal price, to make way for our new top-of-the-line slipstream engines.

In addition, we're offering plasma conduit at the same discount!

Come visit us online at discount-space.zorg while the sale lasts!"

Ok, now that we know what to do, here's one way to do it:

var email_customers =  
    (customers) =>
        customers.map(
            (customer) => ({
                email_address: customer.email,
                subject: "Warp Cores and plasma conduits 70% off this Sunday!",
                body: `Hi ${customer.first_name},`
                    + ""
                    + "This Sunday only we're offering standard CZ-39 warp engine cores at 30% of their normal  price, to make way for our new top-of-the-line slipstream engines."
                    + "In addition, we're offering plasma conduit at the same discount!"
                    + ""
                    + "Come visit us online at discount-space.zorg while the sale lasts!"
            }))
            .forEach(
                (email_data) =>
                    mail(
                        email_data.email_address,
                        email_data.subject,
                        email_data.body))

This function takes an array of customers, maps them to an array of objects suitable for use with the mail function and uses a forEach to call mail on each object.

If we give this function the following data:

var customers = [  
    {
        first_name: 'Joe',
        last_name: 'Bob',
        email: 'joe@joebob.org'
    },
    {
        first_name: 'Spider',
        last_name: 'Man',
        email: 'spiderman@joebob.org'
    },
    {
        first_name: 'Bruce',
        last_name: 'Wayne',
        email: 'brucy@waynecorp.com'
    },
    {
        first_name: 'Clark',
        last_name: 'Kent',
        email: 'clark.kent2@thedailyplanet.com'
    },
    {
        first_name: 'Lois',
        last_name: 'Lane',
        email: 'lois.lane@thedailyplanet.com'
    }
]

We'll get the following result:

------------
joe@joebob.org  
------------
Warp Cores and plasma conduits 70% off this Sunday!  
------------
Hi Joe,This Sunday only we're offering standard CZ-39 warp engine cores at 30% of its normal price, to make way for our new top-of-the-line slipstream engines.In addition, we're offering plasma conduit at the same discount!Come visit us online at discount-space.zorg while the sale lasts!  
------------

------------
spiderman@joebob.org  
------------
Warp Cores and plasma conduits 70% off this Sunday!  
------------
Hi Spider,This Sunday only we're offering standard CZ-39 warp engine cores at 30% of its normal price, to make way for our new top-of-the-line slipstream engines.In addition, we're offering plasma conduit at the same discount!Come visit us online at discount-space.zorg while the sale lasts!  
------------

------------
brucy@waynecorp.com  
------------
Warp Cores and plasma conduits 70% off this Sunday!  
------------
Hi Bruce,This Sunday only we're offering standard CZ-39 warp engine cores at 30% of its normal price, to make way for our new top-of-the-line slipstream engines.In addition, we're offering plasma conduit at the same discount!Come visit us online at discount-space.zorg while the sale lasts!  
------------

------------
clark.kent2@thedailyplanet.com  
------------
Warp Cores and plasma conduits 70% off this Sunday!  
------------
Hi Clark,This Sunday only we're offering standard CZ-39 warp engine cores at 30% of its normal price, to make way for our new top-of-the-line slipstream engines.In addition, we're offering plasma conduit at the same discount!Come visit us online at discount-space.zorg while the sale lasts!  
------------

------------
lois.lane@thedailyplanet.com  
------------
Warp Cores and plasma conduits 70% off this Sunday!  
------------
Hi Lois,This Sunday only we're offering standard CZ-39 warp engine cores at 30% of its normal price, to make way for our new top-of-the-line slipstream engines.In addition, we're offering plasma conduit at the same discount!Come visit us online at discount-space.zorg while the sale lasts!  
------------

Looking for a software developer?