我想知道Grep和Filter之间的区别:
Filter :
Reduce the set of matched elements to those that match the selector or pass the function's test.
格雷普:
Finds the elements of an array which satisfy a filter function. The original array is not affected.
ok.
因此,如果我在grep中执行此操作:
var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];
myNewArray= jQuery.grep(arr, function(n, i){
return (n != 5 && i > 4);
});
我还可以做:
var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];
myNewArray= $(arr).filter( function(n, i){
return (n != 5 && i > 4);
});
在这两种情况下,我仍然可以访问原始array.
so...where is the difference ?