In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand jQuery array processing, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
The array processing of jQuery is convenient and functional. Recent projects use a lot of practical, one-step encapsulation of a lot of native JavaScript array can not reach the function. Recently, time is tight. Today, I took some time to look back at the introduction of arrays in jQuery Chinese documents, and by the way, make a summary of jQuery arrays, reviewing the old and learning the new.
1. $.each (array, [callback]) traversal [commonly used]
Explanation: unlike the $(). Each () method that iterates through jQuery objects, this method can be used to instantiate any object. The callback function has two parameters: the index of the member or array of the object, and the second is the corresponding variable or content. If you need to exit the each loop to make the callback function return false, other return values will be ignored.
Each traversal, I believe is no stranger, in normal event handling, is a variant of the for loop, but more powerful than the for loop. In an array, it can easily capture the array index and the corresponding values. Example:
Var _ mozi= ['Mohist', 'Mozi', 'love but not attack', 'Shangtong Shangxian']; / / the array used in this article is the same as $.each (_ mozi,function (key,val) {/ / callback function has two parameters, * is the element index, and the second is the current value alert (in the'_ mozi array, the corresponding value of index:'+ key+' is:'+ val) })
A little stronger than the native for..in,each. For..in can also traverse the array and return the corresponding index, but the value needs to be obtained through arrName [key].
2. $.GREP (array, callback, [invert]) filter array [commonly used]
Explanation: use the filter function to filter array elements. This function passes at least two parameters (the third parameter is true or false, and it is not useful to invert the return value of the filter function): the array to be filtered and the filter function. The filter function must return true to retain the element or false to delete the element. In addition, the filter function can also be set to a string of words.
$.grep (_ mozi,function (val,key) {/ / filter function takes two parameters, * is the current element, and the second is the element index if (val==' Mozi') {alert ('the subscript of the array is Mozi:' + key);}}); var _ moziGt1=$.grep (_ mozi,function (val,key) {return key > 1;}) Alert (elements in the'_ mozi array whose index value is greater than 1 are:'+ _ moziGt1); var _ moziLt1=$.grep (_ mozi,function (val,key) {return key > 1;}, true); / / the third reliable parameter is passed here, which takes the inverse alert for the return value in the filter function (the element in the'_ mozi array whose index value is less than or equal to 1 is:'+ _ moziLt1)
3. $.map (array, [callback]) converts the array according to the given condition [general]
Explanation: the conversion function as an argument is called for each array element, and the conversion function is passed a representation of the converted element as the parameter. The conversion function can return the converted value, null (delete items in the array), or an array containing values, and extend to the original array.
This is a powerful method, but it is not commonly used. It can update the array element value according to specific conditions, or extend a new copy element based on the original value.
Var _ mapArrA=$.map (_ mozi,function (val) {return val+' [new]';}); var _ mapArrB=$.map (_ mozi,function (val) {return val==' Mozi'? '[add Mozi only]' + val: val;}); var _ mapArrC=$.map (_ mozi,function (val) {/ / extend a new element return [val, (val+' [extension]')];}); alert ('add\' [new]\ 'character after each element to the array:' + _ mapArrA) Alert ('the array after only adding characters to the element Mozi is:' + _ mapArrB); alert ('for each element in the original array, extend an element with the added character\' [new]\', and the returned array is'+ _ mapArrC)
4. $.inArray (val,array) determines whether the value exists in the array [commonly used]
Explanation: determine the position of * parameters in the array, counting from 0 (return-1 if not found).
Remember the indexOf () method? IndexOf () returns the * * occurrence position of the string, while $.inArray () returns the position of the incoming parameter in the array. Similarly, if found, it returns a value greater than or equal to 0, and-1 if it is not found. Now, you know how to use it. With it, it becomes easy to determine whether a value exists in an array.
Var _ exist=$.inArray ('Mozi', _ mozi); var _ inexistence=$.inArray ('Wei Yang', _ mozi) if (_ exist > = 0) {alert ('Mozi exists in the array _ mozi, whose index value in the array is:' + _ exist);} if (_ inexistence)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.