grep()方法用于数组元素过滤筛选

grep(array,callback,invert)
array:待过滤数组;
callback:处理数组中的每个元素,并过滤元素,该函数中包含两个参数,第一个是当前数组元素的值,一个是当前数组元素的下标,即元素索引值。此函数应返回一个布尔值。另外,此函数可设置为一个字符串,当设置为字符串时,将视为“lambda-form”(缩写形式?),其中 a 代表数组元素,i 代表元素索引值。如“a > 0”代表“function(a){ return a > 0; }”
invert:布尔型可选项,默认值false,值为true或false, 如果 “invert” 为 false 或为设置,则函数返回数组中由过滤函数返回 true 的元素,当”invert” 为 true,则返回过滤函数中返回 false 的元素集。
var arr=$.grep([0,1,2,3,4,5,6],function(n,i){
return n>2
});
上面的例子返回[3,4,5,6],但是我们给invert的值为true,例如
var arr=$.grep([0,1,2,3,4,5,6],function(n,i){
return n>2
},ture);
所以现在返回的是[0,1,2],也就是被callback函数过滤掉的元素。
 
  1. var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];
  2. arr = jQuery.grep(arr, function( n, i ) {
  3. if( n !== 5 && i > 4){
  4. return true;
  5. }
  6. });
  7. 等价于:
  8. arr = jQuery.grep(arr, function( n, i ) {
  9. return ( n !== 5 && i > 4 );
  10. });

OA差旅用到该函数来筛选起飞时间:

  1. define(function (require, exports, module) {
  2. var init = function() {
  3. //outsideArr是要去进行筛选的原始数组
  4. var outsideArr=[
  5. {
  6. 'depTime':0020,
  7. 'name':'xiaohua'
  8. },{
  9. 'depTime':0050,
  10. 'name':'dahua'
  11. },{
  12. 'depTime':0025,
  13. 'name':'laohua'
  14. }
  15. ]
  16. //innerArr为筛选条件数组
  17. var innerArr=[
  18. {
  19. 'start':0010,
  20. 'end':0030
  21. }
  22. ];
  23. var innerArrNew={
  24. 'start':0010,
  25. 'end':0030
  26. };
  27. //$.grep()函数中,第一个参数为数组,第二个参数为true时,返回数组,
  28. //每次取出数组outsideArr的一个对象和筛选数组innerArr循环进行判断
  29. //一旦该数组outsideArr的该对象满足筛选数组innerArr的任何一个条件,则flag=true
  30. //所以当flag=true时,返回outsideArr的数组该元素
  31. var ephemeralsec = $.grep(outsideArr, function (item, index) {
  32. var flag = false;
  33. $.each(innerArr, function(i, subItem) {
  34. if (item.depTime > subItem.start && item.depTime <= subItem.end) {
  35. flag = true;
  36. }
  37. });
  38. if (flag) {
  39. return true;//返回值为true时,相当于返回的是outsideArr数组中的item
  40. }
  41. });
  42. //如果筛选条件不是数组,则变成我们常见的形式,没有对里面筛选条件的循环
  43. var ephemeralthree = $.grep(outsideArr, function (item, index) {
  44. if (item.depTime > innerArrNew.start && item.depTime <= innerArrNew.end) {
  45. return true;
  46. }
  47. });
  48. console.log(ephemeralthree);
  49. };
  50. module.exports = init;
  51. });

用图片显示:

// 选择航空公司筛选(和上面时间筛选不一样,是因为时间要筛选一个范围的值,而下面的筛选选择包含的值)

  1. // 选择出发机场筛选
  2. if (filterselected.depAirdrome.length > 0){
  3. ephemeral = $.grep(ephemeral, function (item, index) {
  4. if($.inArray(item.depAirdrome, filterselected.depAirdrome) != -1) {
  5. return true;
  6. }
  7. });
  8. }

随机推荐

  1. C# Arc Gis实例教程——网络链接

    http://www.doc88.com/p-305888679879.html http://www.doc88.com/p-992232217007.html http://www.cnblogs ...

  2. English trip -- Review Unit2 At school 在学校

    What do you need,Loki? I need an eraser What does he need? He needs a dictionary Where's my pencil? ...

  3. 微信小程序自定义模态框(字体图标)

    组件已经传到github,自行下载:https://github.com/JinZhenZon/miniapp-customModel 支持如下配置: {  outWidth <number&g ...

  4. ccf窗口

    #include<iostream> #include<cstring> #include<algorithm> #include<vector> us ...

  5. json 的样式与应用 - C#/.NET

    本文采用问答的方式来写 问题一:什么是 json ? json是一种轻量级的数据交换格式,非常适合服务器与JavaScript交互.(它和XML一样,都是用来处理交互数据的) 问题二:json 长什么 ...

  6. Activiti工作流笔记(2)

    1.Activiti工作数据表 Activiti用来存放流程数据的表共使用23张表,表名都是以"ACT_"开头,底层操作默认使用mybatis操作 工作流Activiti的表是用来 ...

  7. iOS UI-UIScrollView控件实现图片轮播 (UIPageControl-分页指示器)

    一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "ViewController.h" #define HM ...

  8. idea Exception in thread "http-apr-8080-exec-2" java.lang.OutOfMemoryError: PermGen space

    idea Exception in thread "http-apr-8080-exec-2" java.lang.OutOfMemoryError: PermGen space ...

  9. linux pipes

    In Linux, a pipe is implemented using two filedata structures which both point at the same temporary ...

  10. 手机端的META你知道多少

    一.天猫 <title>天猫触屏版</title> <meta content="text/html; charset=utf-8" http-equ ...