$.grep()的用法
grep()方法用于数组元素过滤筛选
- var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];
- arr = jQuery.grep(arr, function( n, i ) {
- if( n !== 5 && i > 4){
- return true;
- }
- });
- 等价于:
- arr = jQuery.grep(arr, function( n, i ) {
- return ( n !== 5 && i > 4 );
- });
OA差旅用到该函数来筛选起飞时间:
- define(function (require, exports, module) {
- var init = function() {
- //outsideArr是要去进行筛选的原始数组
- var outsideArr=[
- {
- 'depTime':0020,
- 'name':'xiaohua'
- },{
- 'depTime':0050,
- 'name':'dahua'
- },{
- 'depTime':0025,
- 'name':'laohua'
- }
- ]
- //innerArr为筛选条件数组
- var innerArr=[
- {
- 'start':0010,
- 'end':0030
- }
- ];
- var innerArrNew={
- 'start':0010,
- 'end':0030
- };
- //$.grep()函数中,第一个参数为数组,第二个参数为true时,返回数组,
- //每次取出数组outsideArr的一个对象和筛选数组innerArr循环进行判断
- //一旦该数组outsideArr的该对象满足筛选数组innerArr的任何一个条件,则flag=true
- //所以当flag=true时,返回outsideArr的数组该元素
- var ephemeralsec = $.grep(outsideArr, function (item, index) {
- var flag = false;
- $.each(innerArr, function(i, subItem) {
- if (item.depTime > subItem.start && item.depTime <= subItem.end) {
- flag = true;
- }
- });
- if (flag) {
- return true;//返回值为true时,相当于返回的是outsideArr数组中的item
- }
- });
- //如果筛选条件不是数组,则变成我们常见的形式,没有对里面筛选条件的循环
- var ephemeralthree = $.grep(outsideArr, function (item, index) {
- if (item.depTime > innerArrNew.start && item.depTime <= innerArrNew.end) {
- return true;
- }
- });
- console.log(ephemeralthree);
- };
- module.exports = init;
- });
用图片显示:
// 选择航空公司筛选(和上面时间筛选不一样,是因为时间要筛选一个范围的值,而下面的筛选选择包含的值)
- // 选择出发机场筛选
- if (filterselected.depAirdrome.length > 0){
- ephemeral = $.grep(ephemeral, function (item, index) {
- if($.inArray(item.depAirdrome, filterselected.depAirdrome) != -1) {
- return true;
- }
- });
- }
随机推荐
- C# Arc Gis实例教程——网络链接
http://www.doc88.com/p-305888679879.html http://www.doc88.com/p-992232217007.html http://www.cnblogs ...
- 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? ...
- 微信小程序自定义模态框(字体图标)
组件已经传到github,自行下载:https://github.com/JinZhenZon/miniapp-customModel 支持如下配置: { outWidth <number&g ...
- ccf窗口
#include<iostream> #include<cstring> #include<algorithm> #include<vector> us ...
- json 的样式与应用 - C#/.NET
本文采用问答的方式来写 问题一:什么是 json ? json是一种轻量级的数据交换格式,非常适合服务器与JavaScript交互.(它和XML一样,都是用来处理交互数据的) 问题二:json 长什么 ...
- Activiti工作流笔记(2)
1.Activiti工作数据表 Activiti用来存放流程数据的表共使用23张表,表名都是以"ACT_"开头,底层操作默认使用mybatis操作 工作流Activiti的表是用来 ...
- iOS UI-UIScrollView控件实现图片轮播 (UIPageControl-分页指示器)
一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "ViewController.h" #define HM ...
- 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 ...
- linux pipes
In Linux, a pipe is implemented using two filedata structures which both point at the same temporary ...
- 手机端的META你知道多少
一.天猫 <title>天猫触屏版</title> <meta content="text/html; charset=utf-8" http-equ ...