Home >Web Front-end >JS Tutorial >jquery methods for traversing arrays and filtering arrays_jquery

jquery methods for traversing arrays and filtering arrays_jquery

WBOY
WBOYOriginal
2016-05-16 17:17:321380browse
grep
grep() method is used to filter array elements
grep(array,callback,invert)
array: array to be filtered;
callback: process each element in the array elements, and filter the elements. This function contains two parameters, the first is the value of the current array element, and the other is the subscript of the current array element, that is, the element index value. This function should return a boolean value. Alternatively, this function can be set to a string, and when set to a string, is treated as a "lambda-form" (short form?), where a represents the array element and i represents the element index value. For example, "a > 0" represents "function(a){ return a > 0; }"
invert: Boolean optional, default value false, value is true or false, if "invert" is false or If set, the function returns the elements in the array that are returned true by the filter function. When "invert" is true, the set of elements that are returned false by the filter function is returned
Copy code The code is as follows:


< script type="text/javascript">
$().ready(
function(){
var array = [1,2,3,4,5,6,7,8,9 | ; I & lt; Filterary.Length; I) {
Alert (Filteraray [i]);
}
for (key in Filteraray) {
Alert (Filteraray [Key]);
}
}
);





each

How to use each
is available in jQuery An each method is very comfortable to use. You don’t need to write a for loop like before. There are many uses of each method in the jQuery source code. In fact, the each method in jQuery is implemented through the call method in js.



Copy code
The code is as follows:

map
map() passes each element into the current matching set through the function and generates a new jQuery object containing the return value.
Since the return value is an array encapsulated by jQuery, use get() to process the returned object to get the underlying array. The
.map() method is particularly useful for getting or setting the value of a set of elements. Consider the following form with a series of checkboxes
Copy code The code is as follows:






Traverse the json object:


Copy code The code is as follows: <script></div>var json = [{dd:'SB',AA:'东东',re1:123},{cccc:'dd',lk:'1qw'}]; <div class="codebody" id="code85758">for(var i=0,l=json.length;i<l;i ){<br> for(var key in json[i]){<br> alert(key ':' json[i] [key]);<br> }<br>}<br></script>



Traversing json objects

has the following json objects:

Copy code The code is as follows:var obj ={"name":"Feng Juan" ,"password":"123456","department":"Technical Department","sex":"Female","old":30};
Traversal method:
for(var p in obj){
str = str obj[p] ',';
return str;
}

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