$.each(array, [callback]) traversal, very commonly used
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
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
//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
//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
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
// $.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.