Home  >  Article  >  Web Front-end  >  What is the usage of map in jquery

What is the usage of map in jquery

WBOY
WBOYOriginal
2022-03-16 15:01:241831browse

In jquery, the map() method is used to perform function processing on each element obtained. You can use the specified function to process each element in the array or each attribute of the object, and encapsulate the processing results. Return a new array, the syntax is "$.map (object or array to be processed, specified processing function)".

What is the usage of map in jquery

The operating environment of this tutorial: windows10 system, jquery1.10.2 version, Dell G3 computer.

What is the usage of map in jquery

It is generally used to perform function processing for each element obtained

$.map() function is used to process the array using the specified function Each element in (or each attribute of the object), and the processing result is encapsulated as a new array and returned.

The syntax is:

$.map( object, callback )

object Array/Object type specifies the array or object that needs to be processed.

callback The processing function specified by Function type.

The example is as follows:

<html>
<head>
<meta charset="utf-8">
<title>123</title>  
<style>
    div { color:blue; }
    p { color:green; margin:0; }
    span { color:red; }
</style>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div></div>
<p></p>
<span></span>
<script>
$(function () { 
    var arr = [ "a", "b", "c", "d", "e" ];
    $("div").text(arr.join(", "));
    arr = $.map(arr, function(n, i){
        return (n.toUpperCase() + i);
    });
    $("p").text(arr.join(", "));
    arr = $.map(arr, function (a) {
        return a + a;
    });
    $("span").text(arr.join(", "));
})
</script>
 
</body>
</html>

Output result:

What is the usage of map in jquery

Related video tutorial recommendation: jQuery video tutorial

The above is the detailed content of What is the usage of map in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn