how grep () is used:
grep (Array,callback,invert)
- Array: arrays to be filtered;
- Callback: Processes each element in the array and filters the element, which 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. In addition, this function can be set to a string that, when set to a string, is considered "Lambda-form" (abbreviated form?). ), where a represents an array element, and I represents the index value of an element. such as "a > 0" means "function (a) {return a > 0;}"
- Invert: boolean selectable, default value false, value TRUE or FALSE, if "invert" is false or set, the function returns the element in the array that is true by the filter function, and returns false in the return filter function when "invert" is true Set of elements.
After explaining the use of grep (), here's a small example:
var arr=$.grep ([0,1,2,3,4,5,6],function (N,i) {
Return n>2
});
The above example returns [3,4,5,6], but we give invert a value of true, for example
var arr=$.grep ([0,1,2,3,4,5,6],function (N,i) {
Return n>2
},ture);
So now the return is [0,1,2], which is the element that is filtered out by the callback function.
Original: http://www.css88.com/archives/2472;
Zepto.js also has this function inside.
Use the same
$.grepv1.0+
⇒ array
Gets a new array that contains only the array items that return ture in the callback function.
$.grep([1,2,3],function(item){ return item > 1});//=>[2,3]
Zepto Document Address: Http://www.css88.com/doc/zeptojs_api/#$.grep
grep () of JQ; Array filtering method