Home > Web Front-end > JS Tutorial > Summary of sample codes for jquery traversal array methods and simple traversal of json objects

Summary of sample codes for jquery traversal array methods and simple traversal of json objects

伊谢尔伦
Release: 2017-07-17 14:18:22
Original
1600 people have browsed it

jquery grep() filterTraverse the array

$().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]); 
} 
} 
);
Copy after login


jquery each() filter and traverse the array

$().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); 
} 
); 
} 
);
Copy after login


jquery inArray() filters and traverses the array

$().ready( 
function(){ 
var anArray = [&#39;one&#39;,&#39;two&#39;,&#39;three&#39;]; 
var index = $.inArray(‘two&#39;,anArray); 
alert(index);//返回该值在数组中的键值,返回1 
alert(anArray[index]);//value is two 
} 
);
Copy after login


jquery map() filters and traverses the array

$().ready( 
function(){ 
var strings = [&#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]); 
} 
} 
);
Copy after login


js traverses and parses jsonobject 1

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]); 
} 
}
Copy after login


js traverses and parses json objects 2

There are the following json objects:
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]+&#39;,&#39;; 
return str; 
}
Copy after login

JS simpleLoopTraverse json Array methods.

For example, the jsonstring in the database is like this

var str = &#39;[{"name":"宗2瓜","num":"1","price":"122"},{"name":"宗呱呱","num":"1","price":"100"}]&#39;;
var xqo = eval(&#39;(&#39; + str + &#39;)&#39;);
for(var i in xqo){
  alert(xqo[i].name);
}
Copy after login

The above is js, and the following is jquery to parse the json string,

var cc = jQuery.parseJSON(data);
alert(cc[0].title);
Copy after login

For looping, it’s the same as above

The above is the detailed content of Summary of sample codes for jquery traversal array methods and simple traversal of json objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template