JavaScript Array map()

📔 : Java Script 🔗

JavaScript Array map() function का use एक callback function के through Array के हर एक element को visit करने के लिए किया जाता है।


map() function Array के प्रत्येक value को user defined callback function में send करता है , और एक Array नई values के साथ return करता है।

Understanding Callback Function

Callback Functions वो function होते हैं जो किसी दूसरे function में as an arguments pass किये जाते हैं। और जिस function में ये pass किये जाते हैं उन्हें Higher - Order Function कहते हैं।
JavaScript Callback Functions


JavaScript Array map() Syntax

array.map(function(currentValue, index, array), value);

Parameters

  1. function(currentValue, index, array) | required : यह callback function Array के हर एक element के लिए run होता है। इसमें तीन Parameters pass किये जाते हैं।

    1. currentValue | required : Array element की current value . 
    2. index | optional : current element की index .
    3. array | optional : यह array object है , जिससे current element belong करता है।
  2. value | optional : यह value callback function में pass करने के लिए होती है , जिसे function में this variable के through access कर सकते हैं , अगर value pass नहीं करते हैं तो by default "undefined" जाता है।

  3. Return Value : यह एक new Array return करता है , जिसका हर एक element को callback function द्वारा generate किया जाता है।


JavaScript Array map()

File : js_array_map.html

CopyFullscreenClose FullscreenRun
<!DOCTYPE html>
<html>
  <body>
    <script>

      function myfun(value , index, arr)
      {
        return value*5;
      }
      var arr=[2, 5, 6, 8, 8];
      let new_arr = arr.map(myfun, 'my value'); 
      document.writeln(new_arr);
    </script>
  </body>
</html>
Output
10,25,30,40,40

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook