The use of the map function in jquery _jquery

Source: Internet
Author: User

It's been a long time since the map () function, because recently read a react article, which has talked about the map () This function, then has looked back some data, discovered that the map () function may use in the different place, the following small series summarizes has the content to share to everybody, also facilitates the future search.

In fact, I have rarely used the map () This function, because recently read a react article, actually talked about the map () function, so I also looked up some data, found that the map () function can also be used in different places:

The map () function passes each element through a function to the current matching collection, generating a new jquery object that contains the return value

Syntax:. Map (Callback (index,domelement))

Callback (index,domelment) "The function object that is invoked for each element in the current collection;

Such as:

 
 

Js:

var result=$ ("ul Li"). Map (function (i,val) { 
console.log (i,val);/return 
this.id;});

Explain:

Console.log (I,val);

Print as shown in the picture:


Console.log (typeof result)//---Of course, if this result type is printed as Object,
Anyway,it is ok!let us go on!

since the return value is an array of jquery encapsulation, we use get () to process the returned object to obtain the underlying array ;

PS: The Get () method obtains the DOM element syntax specified by the selector as follows:

$ (selector). get (index); index: Optional. The specified acquisition of which matching element (through the index number);

So, the code continues:

var result=$ ("ul Li"). Map (function (i,val) { 
console.log (i,val); 

At this time, Result.get () will get the result as shown:


PS: It will get a one-dimensional array, and there may be situations in which you will need to have this effect, as long as you use the Get () method at the right time!

Of course, we can draw the concatenation join () method to pull off the array;

var result=$ ("ul Li"). Map (function (i,val) { 
console.log (i,val); 
return this.id;}); Console.log (typeof result)//---Objectconsole.log (result.get ());

As shown in figure:

Finally, summarize some: inside the callback function, this refers to the current DOM element for each iteration. The function can return a separate data item, or an array of data items to be inserted into the result set. If an array is returned, the elements within the array are inserted. If the function returns null or undefined, no elements are inserted.

The above is a small set to introduce the jquery in the use of the map function, we hope to help everyone!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.