0

I'm learning about the map() method right now and I understand very basic examples.

var numbers = [2, 4, 6];

var double = numbers.map(function(value) {
    return value * 2;
});

My question is, in what cases do developers use the map() method to help solve problems? Are there some good resources with real world examples?

Thanks for the help!

6

3 Answers 3

2

As @Tushar referred:

The map() method creates a new array with the results of calling a provided function on every element in this array.

So it is basically used when you need to apply certain functionality to every single element of an array and get the result back as an array with the new results.

For example doubling the numbers:

var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
  return num * 2;
});
// doubles is now [2, 8, 18]. numbers is still [1, 4, 9]
Sign up to request clarification or add additional context in comments.

Comments

0

It basically helps to shorten your code eliminating the need of using for loop. But do remember it is used when every element of the array is manipulated because map() generates similar length of array provided.

For eg.- in the example you provided doubles will have [2, 8, 18].

where 2 correspond to 1. 4 correspond to 8. 9 correspond to 18.

Comments

0

I recommend you to watch the whole video but your answer is at the 14th minute:

Asynchronous JavaScript at Netflix by Matthew Podwysowski at JSConf Budapest 2015

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.