Map each item in an array

Description

.map() accepts an array or array-like object as its first argument and a callback function.

The callback function is a function to process each item against - accepts one of the array elements and its current index.

Example


<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">     
      $.ready(function(){<!--from  w  ww  .  j  a v  a  2s. com-->
          var numArray = [ 4,1024,2048,4096];
          console.log( numArray );
          var newArray = $.map(numArray, function(value, index){
               return Math.sqrt( value ).toFixed(4);
          });
          console.log( newArray );
      });
    </script>
  </head>
  <body>  
  </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities