map() method in JavaScript

map() method in JavaScript

Let's see what MDN has to say:

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

Here the calling array is the original array.

Let's see some examples in order to understand better:

let's see some examples

Example 1: Double the value of each element in an array and return a new array of modified elements.

//using arrow function

const numbersArray = [1, 5, 22, 40, 19]
const doublesArray = numbersArray.map(item => item*2)
console.log(doublesArray)

Result: [2, 10, 44, 80, 38]


//With normal functions

const numbersArray = [1, 5, 22, 40, 19]
function double(num){
       return num*2;
}
const doublesArray = numbersArray.map(double)
console.log(doublesArray)

Result: [2, 10, 44, 80, 38]        

In the above example, each and every element (or item) of the numbersArray will pass through the function double to return a new value. Further, all the returned values are combined to form a new array.

Note: map method does not mutate the original array. It returns a modified copy of the original array.

[Click to continue reading...]

要查看或添加评论,请登录

Rajat Gupta的更多文章

  • pseudo classes in css part 1 (:hover)

    pseudo classes in css part 1 (:hover)

    Note: This is the first part of the series dedicated to the pseudo-classes of CSS. In this part, we'll understand the…

  • useContext in react: everything you need to know

    useContext in react: everything you need to know

    only understand this: Using useContext is not necessary, we use it to avoid prop drilling. Here's how prop drilling…

  • Sibling combinator in css

    Sibling combinator in css

    There may be some confusion as what we are calling sibling. So, let's first get that out of the way.

  • rest parameter in javascript

    rest parameter in javascript

    The rest parameter is introduced in ES6. It allows a function to accept an indefinite number of arguments as an array…

    1 条评论
  • PropTypes in react

    PropTypes in react

    Let's see what reactjs.org has to say: As your app grows, you can catch a lot of bugs with type-checking.

  • What the heck are props in react

    What the heck are props in react

    Although we can make web apps using JavaScript. One of the reasons we are using react over JS is component reusability.

  • JavaScript: single-threaded and synchronous

    JavaScript: single-threaded and synchronous

    A few days ago single threaded and synchronous were just 2 heavy words for me. If this is you right now, don't worry…

    1 条评论
  • reduce() method in JavaScript

    reduce() method in JavaScript

    Let's see what MDN has to say: The reduce() method executes a user-supplied “reducer” callback function on each element…

  • filter() method in JavaScript

    filter() method in JavaScript

    Definition by MDN: The filter() method creates a new array with all elements that pass the test implemented by the…

  • object-fit property in CSS

    object-fit property in CSS

    The object-fit CSS property sets how the content which includes images, videos and other embedded media formats should…

社区洞察

其他会员也浏览了