Home > Web Front-end > Front-end Q&A > What is the usage of map in jquery

What is the usage of map in jquery

WBOY
Release: 2022-03-16 15:07:27
Original
1868 people have browsed it

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 )
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template