Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use map in jquery

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use map in jquery". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use map in jquery" can help you solve the problem.

Usage: 1, for passing elements to the collection through the function to generate a new jQuery object, syntax for ".map (callback (index,domElement))"; 2, for processing elements in the array and encapsulating the results as a new array return, syntax for "$.map (array or object, specify function)".

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

What is the use of jquery in map?

1. Map () passes each element to the current matching set through a function, generating a new jQuery object containing the return value.

Grammar

.map (callback (index,domElement))

Function object called by callback (index,domElement) for each element in the current collection.

Because the return value is an array encapsulated by jQuery, use get () to process the returned object to get the underlying array.

Examples are as follows:

P {color:red;}

Values:

$("p") .append ($("input") .map (function () {return $(this). Val ();}) .get () .join (","))

Output result:

2. 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.

Prior to jQuery 1.6, this function only supported traversing arrays; since 1.6, it also supports traversing objects.

Map () also passes in two parameters to the function: the element or attribute value of the current iteration and the array index or object property name of the current iteration item.

The function returns a value as an element in the result array, and if the return value is null or undefined, it will not be added to the result array.

$.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:

123div {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 introduction to "how to use map in jquery". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report