A brief introduction to the map () function
The map () function has always been one of the more useful functions I think, why do you say so?
First, consider whether you have encountered the following scenario: You need to traverse a set of objects to fetch a property (such as an ID) of each object and separate it with a delimiter
I think you must have met it! Because I've met a lot of times, this is a typical scenario for the map () function
Not only in the foreground, the background is also (so I in Java in the simple implementation of such a method), let's see below
Map () Function instance
In jquery, like . each () , the. Map () function also has two forms: one that is provided to the JQuery object . Map (), and one for the normal array . Jquery.map ()
Jquery.map ()
Both are very simple to apply, first take jquery.map () , we put forward the scene: for example, in the Easyui of the DataGrid, we need to implement changes in the status of multiple records, then will be checked with a checkbox to select multiple Records, The ID of this set of records and the status values that need to be modified are then uploaded to the background. The process of getting this set of record IDs is a typical application of jquery.map () , because a common set of JSON arrays is taken out of Datagrid.datagrid (' getselections ')
The following code implements the bulk edit employee role:
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, and the definition of the Web is actually: converting all elements in an array into another array.
It's like that! Looking back at the example above, we have converted an array of employee (JSON) into an array of empno (string).
Then look at its callback function, in the callback function of $.map () can have two parameters, starting from jQuery1.6, the first is the currently traversed record, and the second is a numeric index, starting from 0.
You should notice that the return value of the function will be mapped to the final converted array. It is important to note that there are 2 special cases for the return value:
1,null , or undefinedfor removing the element
2, an array that adds elements from the array to the final result array
For these 2 special cases, give 2 more instance codes:
var arr = [{id:0, Name: ' Zhang San ', age:10}, {id:1, Name: ' John Doe ', age:12}, {id:3, name: ' Zhu Six ', Age:10}];var value = $.map (arr, functi On (v) {return v.name = = ' John Doe '? null:v.name;//here excludes John Doe}). Join (); alert (value);//The result here is "Zhang San, zhu Six" value = $.map ([0,1,2], Functi On (n) {
Here is a little more information to add:
In fact, JS has provided the map () function for array, and the effect is similar to $.map (arr, function (v) {})
But in the process of use I found that when return null, the processing of the two are different, such as the above example, excluding John Doe, then the result is "Zhang San, Zhu Six"
And for the map () function in JS, the return is "Zhang San, Zhu Six", see? The seats will still be accounted for, as to the specific use of which depends on the specific logic.
. Map ()
Here's about Jquery,map () here, here is the. Map () function, which says that this function is provided to jquery objects, how to use it?
A common requirement, such as we use a dialog box to provide a set of checkboxes, then we need to put the results into the original interface after the election, this time we need to get the selected object
<div> Vegetable Tofu <input type= "checkbox" value= "1" name= "Dishes" > </div> <div> Hot and 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 selected dishes to form }) </script>
The core of the function of the idea is the same, the specific multi-use will be handy!
Java implementation of the map () function
Do you realize the convenience of the map () function? In Java, it seems to use this method, but the Java API does not provide a similar tool method
Then respond to Chairman Mao's call, "Do your own, clothed" bar, hehe ~
The specific 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 error, this member variable does not exist");}} return null;}
However, this method can be compared with the jquery map () is still a big gap, here is simply the implementation of the map () The simplest function
and can not make arbitrary changes to the obtained value, if you want to implement or use callback interface way to do, here is no longer give code, interested friends can try ~
The Jquery--map () function and its Java implementation