Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use map in jquery

Shulou Source: shulou.com Published: 2022-06-01 05:25:29 10月09日 Update

This article introduces the knowledge of "how to use map in jquery". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In jquery, the use of map () is to use a specified function to handle each element or attribute of an object in an array, and to encapsulate the result of processing as a new array return, with the syntax of "$.map (objects or arrays to be processed, specified handlers)".

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

What is the use of map in jquery

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

The $.map () function is used to process each element in the array (or each attribute of the object) using the specified function and to encapsulate the result as a new array return.

The syntax is:

$.map (object, callback)

The array or object that needs to be processed specified by the object Array/Object type.

The handler specified by the callback Function type.

Examples are as follows:

123 div {color:blue;} p {color:green; margin:0;} span {color:red;}

$(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 (","));})

Output result:

This is the end of the content of "how to use map in jquery". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Processing functions arrays objects elements results content attributes more knowledge types syntax and will encapsulation output utility learning and then dilemma reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information vpn OPPO Reno NVidia