Home>Article>Web Front-end> Is map in jquery returning an object or something?

Is map in jquery returning an object or something?

WBOY
WBOY Original
2022-06-24 17:15:58 2272browse

In jquery, the map() function returns a new encapsulated array using the specified function; this function is used to use the specified function to process each element in the array or each attribute of the object, and The processing result is encapsulated and returned as a new array; the return value of this function will be used as an element in the result array. If the return value is null or undefined, it will not be added to the result array.

Is map in jquery returning an object or something?

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

map in jquery

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

Note:

1. Before jQuery 1.6, this function only supported traversing arrays; starting from 1.6, this function also supports traversing objects.

2. map() will also pass in two parameters to the function: one is the element or attribute value of the current iteration, and the other is the array index or object attribute name of the current iteration item.

3. The return value of this function will be used as an element in the result array. If the return value is null or undefined, it will not be added to the result array.

Syntax

$.map( object, callback )

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

callback Function type specifies the processing function.

Examples are as follows:

    123   

Examples are as follows:

Is map in jquery returning an object or something?

##Expand knowledge:

There is a map method in our native js, and there is also a map method in jQuery. What is the difference between them? As usual, look at the code first


The first parameter: the currently traversed element

The second parameter: the currently traversed index

The third parameter: the currently traversed array

However, note: Like the forEach method of native js, pseudo arrays cannot be traversed

There is no doubt that an error is reported, which proves that the map method of native js cannot traverse pseudo arrays

Then let’s take a look at the map method in jQuery

## The first parameter: the array to be traversed

Second parameter: The callback function executed after each element traversed

Parameters of the callback function:

First parameter: The traversed element

Second Parameters: The traversed index

can traverse the array just like the native js map method

So can it traverse the pseudo array? So let’s get straight to the code!

Obviously it is possible. Just like the each method in jQuery, the map method can also traverse pseudo arrays


Since both the each and map methods in jQuery can traverse arrays and pseudo-arrays, what is the difference between them?

    The default return value of each method is to return whoever is traversed
  1. 2. The default return value of the map method is an empty array





1. The each method does not support processing the traversed array in the callback function
2. The map method can be used in the callback function Process the traversed array through return, and then generate a new array to return



. You can see that the array returned by the map method is the index plus the index. The corresponding value, so the map method can process the traversed array through return, and then generate a new array to return


And each does not support processing the traversed array in the callback function through return
The above comparison should be Now that you understand the difference between them, you should have a clear understanding of when to use the map method and when to use the each method, right?

Video tutorial recommendation:jQuery video tutorial

The above is the detailed content of Is map in jquery returning an object or something?. 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