Home >Web Front-end >JS Tutorial >Several methods of jquery traversing and filtering arrays and traversing and parsing json objects_jquery

Several methods of jquery traversing and filtering arrays and traversing and parsing json objects_jquery

WBOY
WBOYOriginal
2016-05-16 17:09:221545browse

jquery grep() filters and traverses the array

Copy code The code is as follows:

$().ready (
function(){
var array = [1,2,3,4,5,6,7,8,9];
var filterarray = $.grep(array,function(value) {
return value > 5; // Filter out
}) that are greater than 5;
for(var i=0;ialert(filterarray[i ]);
}
for (key in filterarray){
alert(filterarray[key]);
}
}
);

jquery each() filters and traverses the array
Copy code The code is as follows:

$().ready (
function(){
var anObject = {one:1,two:2,three:3};//For json array each
$.each(anObject,function(name,value) {
alert(name);
alert(value);
});
var anArray = ['one','two','three'];
$.each(anArray ,function(n,value){
alert(n);
alert(value);
}
);
}
);

jquery inArray() filters and traverses the array
Copy code The code is as follows:

$(). ready(
function(){
var anArray = ['one','two','three'];
var index = $.inArray('two',anArray);
alert (index);//Returns the key value of the value in the array, returns 1
alert(anArray[index]);//value is two
}
);

jquery map() filters and traverses the array
Copy code The code is as follows:

$() .ready(
function(){
var strings = ['0','1','2','3','4','S','6'];
var values ​​= $.map(strings,function(value){
var result = new Number(value);
return isNaN(result) ? null:result;//isNaN:is the abbreviation of Not a Number
}
);
for (key in values) {
alert(values[key]);
}
}
);

js traverse and parse json object 1
Copy code The code is as follows:

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

js traversal and parsing json object 2

has the following json object:
var obj ={"name":"Feng Juan","password":"123456″,"department":" Technology Department","sex":"female","old":30};
Traversal method:
Copy code The code is as follows:

for(var p in obj){
str = str obj[p] ',';
return str;
}

The following is an example to illustrate the specific implementation method

The way JQuery gets the object

$('#id'): through the id of the element
$('tagName '): Pass the tag name of the element
$('tagName tagName'): Pass the tag name of the element, eg: $('ul li')
$('tagName#id): Pass the id and Tag name
$(':checkbox'): Get all elements whose input type is checkbox':
Eg: value="imperial "/>

$('span[price] input[type=text]'): Get the following input element

< input type="text" name="imperial.quantity"
disabled="disabled" value="1"/>

$('div',$(this) .parents('div:first')): Get the first div node above the div (at least the parent node)
$('~ span:first',this): locates the first sibling of this that's a element.

Lazy loading js file:
$.getScript

Example:
Html file:

Copy code The code is as follows:




$.getScript Example


< script type="text/javascript">
$(function(){
$('#loadButton').click(function(){
$.getScript(//In Firefox/3.0. An error will appear in 1 (syntax error, defined variables do not work, ff2 is fine)
'new.stuff.js'//, function(){$('#inspectButton').click()}
);
});
$('#inspectButton').click(function(){
someFunction(someVariable);
test()
});
});










$.getScript Example

< script type="text/javascript"
src="../scripts/jquery-1.2.1.js">




First
Second
Third
Fourth
Fourth






Hi!




First
Second
Third
Fourth
Fourth



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
Previous article:2 solutions to xmlhttp cache clearing_javascript skillsNext article:2 solutions to xmlhttp cache clearing_javascript skills

Related articles

See more