<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: anandpatel1986</title>
    <description>The latest articles on DEV Community by anandpatel1986 (@anandpatel1986).</description>
    <link>https://dev.to/anandpatel1986</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1082222%2F906e0786-df04-47af-8434-8992e69ce387.jpeg</url>
      <title>DEV Community: anandpatel1986</title>
      <link>https://dev.to/anandpatel1986</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anandpatel1986"/>
    <language>en</language>
    <item>
      <title>JavaScript Array method .map()</title>
      <dc:creator>anandpatel1986</dc:creator>
      <pubDate>Sun, 14 May 2023 09:02:19 +0000</pubDate>
      <link>https://dev.to/anandpatel1986/javascript-array-method-map-a32</link>
      <guid>https://dev.to/anandpatel1986/javascript-array-method-map-a32</guid>
      <description>&lt;p&gt;Hello Everyone, in this post I will try to explain about JavaScript Array map() method with the help of examples.&lt;/p&gt;

&lt;p&gt;The map() method iterates over each element in array and creates new array passing each element to specific function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
General syntax of map() method is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.map(callback(currentValue), thisArg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parameters:&lt;/strong&gt; map() method accepts two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;callback&lt;/code&gt;- This function is called on each element of array and returns value which will be added in new array. It takes in 3 parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;currentValue :  It is a required parameter and it holds the value of the current element.&lt;/li&gt;
&lt;li&gt;index : It is an optional parameter and it holds the index of the current element.&lt;/li&gt;
&lt;li&gt;array : It is an optional parameter and it holds the array.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;thisArg&lt;/code&gt; (optional)- It is used to hold the value passed to the function. By default it is undefined.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Return value:&lt;/strong&gt; It returns a new array with elements as the return values from the &lt;code&gt;callback&lt;/code&gt; function for each element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;map() doen't change the original array.&lt;/li&gt;
&lt;li&gt;it executes &lt;code&gt;callback&lt;/code&gt; for each array element in order&lt;/li&gt;
&lt;li&gt;it does not execute &lt;code&gt;callback&lt;/code&gt; for elements without values.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The below examples illustrate the use of the array map() method in JavaScript: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1 :&lt;/strong&gt;  In below example map() method takes in elements of original array and returns new array with square of elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2, 5, 8];
const sqrNumbers = numbers.map((num) =&amp;gt; num * num);

console.log(numbers); // [2, 5, 8];
console.log(sqrNumbers); // [4, 25, 64];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2 :&lt;/strong&gt;  In below example map() method takes in each city names from names array and returns new array with uppercase city names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const names = ["brampton", "london", "new york"];
const UpperCaseNames = names.map((name) =&amp;gt; name.toLocaleUpperCase());

console.log(names); // ["brampton", "london", "new york"]
console.log(UpperCaseNames); // ["BRAMPTON", "LONDON", "NEW YORK"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 3 :&lt;/strong&gt;  In below example map() method iterates over each employee data and returns netEarning for each employee in newArr.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const employees = [
    { name: "Adam", salary: 5000, bonus: 500, tax: 1000 },
    { name: "Noah", salary: 8000, bonus: 1500, tax: 2500 },
    { name: "Fabiano", salary: 1500, bonus: 500, tax: 200 },
    { name: "Alireza", salary: 4500, bonus: 1000, tax: 900 },
];

// calculate the net amount to be given to the employees
const calcAmt = (obj) =&amp;gt; {
    newObj = {};
    newObj.name = obj.name;
    newObj.netEarning = obj.salary + obj.bonus - obj.tax;
    return newObj;
};

let newArr = employees.map(calcAmt);
console.log(newArr);
/* Output will be : 
[
  { name: 'Adam', netEarning: 4500 },
  { name: 'Noah', netEarning: 7000 },
  { name: 'Fabiano', netEarning: 1800 },
  { name: 'Alireza', netEarning: 4600 }
] */

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion map() method is used to modify each element in array and returns new array with modified elements. &lt;/p&gt;

&lt;p&gt;Thanks for reading this post. &lt;/p&gt;

&lt;p&gt;For more information check below resources:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;MDN-Array.prototype.map()&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/javascript-array-map-method/"&gt;geeksforgeeks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/jsref/jsref_map.asp#:~:text=Definition%20and%20Usage,not%20change%20the%20original%20array."&gt;w3schools&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>arrayiteration</category>
      <category>map</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
