Home > Web Front-end > JS Tutorial > Summary of jQuery array processing methods_jquery

Summary of jQuery array processing methods_jquery

WBOY
Release: 2016-05-16 18:05:52
Original
1094 people have browsed it

$.each(array, [callback]) traversal, very commonly used

Copy code The code is as follows:

var arr = ['javascript', 'php', 'java', 'c ', 'c#', 'perl', 'vb', 'html', 'css', 'objective-c'];
$.each(arr, function(key, val) {
// firebug console
console.log('index in arr:' key ", corresponding value:" val);
// If you want to exit the loop
// return false;
});

$.grep(array, callback, [invert]) filter, commonly used
Copy code The code is as follows:

var temp = [];
temp = $.grep(arr, function(val , key) {
if(val.indexOf('c') != -1)
return true;
// If the [invert] parameter is not given or is false, $.grep only collects callbacks The function returns true array elements
// Otherwise, the [invert] parameter is true, $.grep collects the callback function returns false array elements
}, false);
console.dir(temp);

$.map(array, [callback]) is not used too much
Copy code The code is as follows :

//Versions before 1.6 only support arrays
temp = $.map(arr, function(val, key) {
//Return null, the length of the returned array is reduced 1
if(val === 'vb') return null;
return val;
});
console.dir(temp);
//Json format is supported since 1.6 object
var obj = {key1: 'val1', key2: 'val2', key3: 'val3'};
temp = $.map(obj, function(val, key) {
return val ;
});
console.dir(temp);

$.inArray(val, array) to determine whether it is in the specified array, commonly used
Copy code The code is as follows:

//Return the position of the element in the array, 0 is the starting position, return -1 The element was not found
console.log($.inArray('javascript', arr));

$.merge(first, second) merges two arrays, the frequency of use is generally
Copy code The code is as follows:

var frontEnd = ['javascript', 'css', 'html '],
backEnd = ['java', 'php', 'c '];
// This method will modify the first parameter, which is the frontEnd array
temp = $.merge(frontEnd , backEnd);
console.dir(temp);
console.dir(frontEnd);
// You can use the following method to avoid the impact on the original array
// $.merge( $.merge([], frontEnd), backEnd);

$.unique(array) filters duplicate elements in the array, not commonly used
Copy code The code is as follows:

blahblahblah....



;DIV>

// $.unique only supports DOM element arrays, removes duplicate DOM elements, and does not support other types of arrays (String or Number)
// Get the original DOM array, not jQuery encapsulated
var divs = $('div').get();
// Add a few divs with class dup
divs = divs.concat($('div.dup') .get());
console.log("before unique:" divs.length);
divs = $.unique(divs);
console.log("after unique:" divs.length ;
The code is as follows:

//First of all, what is an array-like object? jQuery official website uses divs = getElementsByTag('div') as an example
//This divs is similar to an array Some methods such as length, obtaining elements through [index], etc.
//Then convert it into an array through $.makeArray(divs), and you can use other functions of the array//Such as reverse(), pop () etc. $(dom).toArray() restores jQuery collection to DOM array, not commonly used

Copy code


The code is as follows:


//Same as makeArray, it is quite uncommon and can be ignored in general This article refers to Mr.Think's blog, Thanks for sharing this
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