JQuery -- map () function and its java implementation, jquerymap

Source: Internet
Author: User

JQuery -- map () function and its java implementation, jquerymap
Introduction to map () Functions

Map () functions have always been one of the more practical functions I think. Why?

First, consider whether you have encountered the following scenarios: You need to traverse a group of objects to retrieve a certain attribute (such as id) of each object and separate them with delimiters.

I think you must have met it! This is a typical application scenario of map () functions.

Not only in the foreground, but also in the background (so I also implemented this method in java later). Let's take a look at it.


Map () function instance

In jQuery, such. the same as each ,. map () functions can also be provided to jQuery objects. map (), there is also a normal array of jQuery. map ()


JQuery. map ()

Both of them are very simple to use. Take jQuery first. for example, in the datagrid of easyUI, if we need to change the status of multiple records, we will use checkbox to check multiple records, in addition, the IDs and Status values of the records to be modified are uploaded to the background. The process of getting this set of record IDS is a typical application of jQuery. map (), because a group of common json arrays is retrieved through the datagrid. datagrid ('getselections ').


The following code implements batch edit of employee roles:

Var rows = $ ('# employee_table '). datagrid ('getselections'); if (rows) {var empnos = $. map (rows, function (v) {return v. empno ;}). join (","); employee_save_url = "test/editEmployee? Empnos = "+ empnos; $ ('# employee_dialog '). dialog ('open '). dialog ('settitle', 'modify employee role '). dialog ('center ');}

This is just a typical application of the map () function. Its definition on the official website is actually:Converts all elements in an array to another array.

It seems like this! Review the preceding example to convert an array consisting of employee (json) into an array consisting of empno (string.

Let's take a look at its callback function, at $. the callback function of map () can have two parameters. Starting from jQuery1.6, the first parameter is the record currently traversed, and the second parameter is the digital index, starting from 0.

You should note that the return value of this function will be mapped to the final converted array. Note that there are two special cases for the returned values:

1. null or undefined, used to remove this element

2. the array adds the elements in the array to the final result array.

Here are two more instance codes for these two special cases:

Var arr = [{id: 0, name: 'zhang san', age: 10}, {id: 1, name: 'Li si', age: 12}, {id: 3, name: 'zhu liu', age: 10}]; var value = $. map (arr, function (v) {return v. name = 'Li si '? Null: v. name; // here Li Si} is excluded }). join (); alert (value); // The result here is "James, James" value = $. map ([0, 1, 2], function (n) {return [n, n + 1];}). join (); alert (value); // The result here is

Here is a little bit of knowledge:

In fact, the map () function has been provided for array in js, and the effect is similar to $. map (arr, function (v) {}).

However, I found that when return is null, there is a difference between the two. For example, in the above example, Li Si is excluded, and the result is "Zhang San, zhu liu"

For the map () function in js, the returned result is "Zhang San, zhu liu". Have you seen it? The seats will still be occupied. The specific logic depends on which one is used.


. Map ()

Here we will talk about jQuery and map (). below is the. map () function, which is provided for jQuery objects. How can this function be used?

A common requirement. For example, if we use a dialog box to provide a set of checkpoints, we need to put the results into the original interface after the selection. At this time, we need to get the selected object.

<Div> green vegetable tofu <input type = "checkbox" value = "1" name = "dishes"> </div> <div> sour potato silk <input type = "checkbox" value = "2" name = "dishes"> </div> <div> braised crucian carp <input type = "checkbox" value = "3" name = "dishes"> </div> <div> spicy chicken <input type = "checkbox" value = "4" name = "dishes"> </div> <script >$ (function () {var ids = $ (': checkbox: checked '). map (function () {return this. value ;}). get (). join (); $ ('# dishes '). val (ids); // Save the selected dishes to the form}) </script>

The core idea of functions is the same, so you can use them with ease!


Java Implementation of map () Functions


Have you realized the convenience of the map () function? This method also seems to be used in Java, but no similar tool or method is provided for searching Java APIs.

Then, let's respond to Chairman Mao's call and say "Do it yourself and make full use of your clothes ~

The Code is as follows:

Public static String map (Collection <?> C, String fieldName) {Field field = null; if (CollectionUtils. isNotEmpty (c) {Class <?> Clazz = c. iterator (). next (). getClass (); try {field = clazz. getDeclaredField (fieldName); field. setAccessible (true); String str = ""; for (Object object: c) {Object val = field. get (object); if (val! = Null) {str + = "," + val. toString () ;}}if (isBlank (str) {return null;} return str. substring (1);} catch (Exception e) {System. out. println ("fieldName filled in incorrectly, this member variable does not exist") ;}} return null ;}

However, this method is quite different from jQuery's map () method. Here we just implement the simplest map () function.

The obtained value cannot be changed at will. If you want to implement it, you can use the callback interface. The Code will not be provided here. If you are interested, you can try it ~





How does jquery create a set similar to a java map?

Var map = {}; // Map map = new HashMap ();
Map [key] = value; // map. put (key, value );
Var value = map [key]; // Object value = map. get (key );
Var has = key in map; // boolean has = map. containsKey (key );
Delete map [key]; // map. remove (key );

How to Use the map function in java?

Map is not a function, but an object.
Map map = new HashMap ();
Map is a key-value pair,
Map. put ("1", "123 ");

Map. put ("2", "234 ");

Map. get ("1") // 123

Map. get ("2") // 234

Related Article

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.