Home  >  Article  >  Web Front-end  >  What are the ways to traverse arrays and objects using jquery

What are the ways to traverse arrays and objects using jquery

伊谢尔伦
伊谢尔伦Original
2017-07-17 14:10:091578browse

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 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, which, 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, the default value is false, the value is true or false, if " "invert" is false or set, the function returns the elements in the array that are returned true by the filter function. When "invert" is true, the function returns the set of elements that are returned false by the filter function

<script type=&#39;text/javascript&#39; src="/jquery.js"></script><script type="text/javascript">
$().ready(
    function(){
        var array = [1,2,3,4,5,6,7,8,9];
        var filterarray = $.grep(array,function(value){
            return value > 5;//筛选出大于5的
        });
        for(var i=0;i<filterarray.length;i++){
            alert(filterarray[i]);
        }
        for (key in filterarray){
            alert(filterarray[key]);
        }
    }
);
</script>

each
How to use each
There is an each method in jQuery, which is very fun to use. You don’t have to write it like the original for loop, 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.

<script type=&#39;text/javascript&#39; src="/jquery.js"></script>
<script type="text/javascript">
$().ready(
    function(){
        var anObject = {one:1,two:2,three:3};//对json数组each
        $.each(anObject,function(name,value) {
            alert(name);
            alert(value);
        });
        var anArray = [&#39;one&#39;,&#39;two&#39;,&#39;three&#39;];
        $.each(anArray,function(n,value){
            alert(n);
            alert(value);
        }
        );
    }
);
</script>



inArray
jQuery.isArray(obj) is new in jQuery 1.3. Tests whether the object is an array. Return value: Boolean
Parameters: objObject Object used to test whether it is an array
Example: Check whether it is an array

<script type=&#39;text/javascript&#39; src="/jquery.js"></script>
<script type="text/javascript">
$().ready(
    function(){
        var anArray = [&#39;one&#39;,&#39;two&#39;,&#39;three&#39;];
        var index = $.inArray(&#39;two&#39;,anArray);
        alert(index);//返回该值在数组中的键值,返回1
        alert(anArray[index]);//value is two
    }
);
</script>



map
map() passes each element into the current matching collection 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

<script type=&#39;text/javascript&#39; src="/jquery.js"></script>
<script type="text/javascript">
$().ready(
    function(){
        var 
string
s = [&#39;0&#39;,&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;S&#39;,&#39;6&#39;];
        var values = $.map(strings,function(value){
                var result = new Number(value);
                return isNaN(result) ? null:result;//isNaN:is Not a Number的缩写
            }
        );
        for (key in values) {
            alert(values[key]);
        }
    }
);
</script>



Traversing the json object:

<script>
var json = [{dd:&#39;SB&#39;,AA:&#39;东东&#39;,re1:123},{cccc:&#39;dd&#39;,lk:&#39;1qw&#39;}];
for(var i=0,l=json.length;i<l;i++){
    for(var key in json[i]){
        alert(key+&#39;:&#39;+json[i][key]);
    }
}
</script>


The above is the detailed content of What are the ways to traverse arrays and objects using jquery. 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