The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

The Array map method

The Array map method

3:02
One very common operation in programming is to iterate through an Array's contents, apply a function to each item, and create a new array containing the results. For example, let's say you wanted to loop through an array of stock objects and select only the name for display on screen. In this lesson we will demonstrate how to use the Array's map method to easily perform this operation with less code than a loop would require.
Watch this lesson now
Avatar
egghead.io

One very common operation in programming is to iterate through an Array's contents, apply a function to each item, and create a new array containing the results. For example, let's say you wanted to loop through an array of stock objects and select only the name for display on screen. In this lesson we will demonstrate how to use the Array's map method to easily perform this operation with less code than a loop would require.

Avatar
Kristofer

Hi, I have a question regarding the symbols object. After the function has run has the values in this object been replaced and only contains the symbols, or does it still contain price and volume?

Avatar
labeeb

Only symbols are in the array.

In reply to Kristofer
Avatar
abhinav reddy

does map,filter and reduce create shallow copy's

In reply to egghead.io
Avatar
Andrejb

A very bad example for beginners. However, master doesn't look anything useful.
Awful example, can't imagine why and for what i pay the money. W3Schools explains better.

Avatar
beckinfonet

Hi,
Could you please explain why:
var symbols = getStockSymbols([
..........])
var is assigned to an array and on line #1 it is function with the same name?

Avatar
Rizwan

Just wondering using similar thing using lodash map, are they asynchronous as well?. How about async library?.

In reply to egghead.io
Avatar
Andrea Di Marco

I think you were just tired when watching this and probably figured it out by yourself :) however on the first line you define the function, which accepts an array of stock objects, and finally, you just invoke that function, passing the whole array manually (pretend you are fetching some data from whatever api), and then storing a reference to the symbols array returned from the function in a variable called "symbols" (the "var symbols..." thing you mentioned ).

In reply to beckinfonet
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?