Jquery .map( callback ) method is used to project the contents that is used in the array and then map this object with the new set of array, It is able to create a new array for calling every elements. It provides function once for each set of an array mapping the content with new array
For example: If a user wishes to process a plain array or objects they can used the .map() function for traversing the whole set of array, This method is generally used for getting and setting the value of an array.
Syntax for using the .map() function:-
selector.map( callback )
Source Code:-
<!DOCTYPE html>
<html>
<head>
<title>Jquery Arrays: Mapping an Array(square) </title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function(){
var numbers = [1, 2, 3, 4, 5, 6, 7];
$("#display").text("The Original Numbers: " + numbers.join(","));
// $.map() method with a function to project the contents of an array into a new array
numbers = $.map(numbers, function(i)
{
return i * i;
});
$("#display2").text("Squared: " + numbers.join(", "));
});
});
</script>
<style>
body{font-family:Verdana; color:#444;}
</style>
</head>
<body>
<h3>Mapping of an Array(Square)</h3>
<div id="display"></div>
<div id="display2"></div>
<button>Click Here</button><br>
</body>
</html>
Output:-
For seeing the result user can download file from the below link.
0 Comment(s)