我想知道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 ?

推荐答案

它们的功能相似,但用法不同.

filter函数用于html元素,这就是为什么它是一个可链接函数,返回jQuery对象,并接受诸如":偶数"、"奇数"或":可见"等过滤器的原因.grep函数是用于数组的实用函数,不能这样做.

Jquery相关问答推荐

当一个很长的活动增加标题时,满历js每周都会出现.这是一种让标题只出现一次的方法吗?

jquery如何判断ajax调用的响应类型

如何像这样通过 $.ajax ( serialize() + extra data ) 添加数据

如何使用 jQuery 禁用粘贴(Ctrl+V)?

什么是不使用 jQuery 的经验技术原因?

仅在 _some 字段上通过 Enter 键禁用表单提交

jQuery scrollTop 在 Chrome 中不工作但在 Firefox 中工作

jQuery切换文本?

使html文本输入字段随着我输入而增长?

DataTables:无法读取未定义的属性长度

C# String.IsNullOrEmpty Javascript 等效项

使用 jquery 通过文本设置下拉值

带有 jQ​​uery 的饼图

判断文本框是否有空值

jQuery ajax 成功回调函数定义

检测用户是否创建了滚动事件

使用 Jquery 查找父 div 的 id

通过 javascript/jquery 删除/截断前导零

如何在文件 Select 上触发事件

jQuery append() 与 appendChild()