JSON对象多个过滤器 [英] JSON Object Multiple filter

查看:86
本文介绍了JSON对象多个过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我JSON对象中的数据被上选择从表单元素进行筛选。
我的表单有以下元素:

年龄下限 - 最大年龄性别 - 男(1)及女(2)

以下是我的JSON对象:

  [
   {
      ID:1
      名:nehil
      性别:1
      生日:1991年7月22日
      business_id:1,
      时间戳:2016年3月23日四时46分42秒,
      时代:24
   },
   {
      ID:2
      名:vartika
      性别:2
      生日:1990年8月14日
      business_id:1,
      时间戳:2016年3月23日4点46分46秒,
       时代:25
   },
   {
      ID:3
      名:阿塔
      性别:1
      生日:1992年10月10日
      business_id:1,
      时间戳:2016年3月23日4时46分49秒,
       时代:23
   },
   {
      ID:4,
      名:卡兰
      性别:1
      生日:1992年12月22日
      business_id:1,
      时间戳:2016年3月23日4时46分52秒,
      时代:23
   }
]

性别选择是否男性,我想 ID 从对象中的所有男性和阵列推。
后来如果我选择最小年龄23岁最大24,我想以下年龄所有男性的阵列中进行更新。

什么将是实现这一目标的最佳策略是什么?

以下是我的小提琴链接 - http://jsfiddle.net/Nehil/2ym3ffo0/4/


解决方案

您可以使用一个对象具有特殊结构的搜索,如果你需要牛逼寻找多个项目。这个提议使用的对象时,具有这种结构用于过滤

  {
    性别:'1',
    年龄:{
        分:23,
        马克斯:24
    },
    生日:功能(S){返回s.match(/ - 10-10 /); }
}

搜索如果的所有的比较为真,则该元素被添加到结果集的算法寻找每一个属性

\r
\r

函数滤波器(数组,搜索){\r
    返回array.filter(函数(){\r
        返回Object.keys(搜索)。每天(函数(K){\r
            返回(\r
                一个[K] ===搜索[K] ||\r
                typeof运算搜索[K] ==='对象'和;&安培; +搜索[K] .min&LT = A [K]&功放;&安培;一个[K]< = +搜索[K]的.max ||\r
                typeof运算搜索[K] ==='功能'和;&安培;搜索[K](一[K])\r
            );\r
        });\r
    });\r
}\r
\r
VAR数据= {[ID:1,名称:nehil性别:1,生日:1991年7月22日,business_id:1,时间戳:2016年3月23日04:46 :42,年龄24},{ID:2,名称:vartika性别:2,生日:1990年8月14日,business_id:1,时间戳:2016年-03-23​​ 4时46分46秒,年龄25},{ID:3,名称:阿塔性别:1,生日:1992年10月10日,business_id: 1,时间戳:2016年3月23日4时46分49秒,年龄:23},{ID:4,名称:卡兰性别:1,生日:1992年至1912年-22,business_id:1,时间戳:2016年3月23日4时46分52秒,年龄:23}];\r
\r
的document.write('< pre>'+ JSON.stringify(过滤器(数据,{生日:功能(S){返回s.match(/ - 10-10 /);}}),0,4 )+'< / pre>');\r
的document.write('< pre>'+ JSON.stringify(过滤器(数据,{性别:'2'}),0,4)+'< / pre>');\r
的document.write('&下; $ P $差异无显着+ JSON.stringify(过滤器(数据,{性别:'1',年龄:{分钟:23,最大:24}}),0,4)+'&下; / pre>');

\r

\r
\r

I have JSON object in which data gets filter on Selecting elements from form. My form has following elements:

Min Age - Max Age and Gender - male(1) & female(2)

Following is my JSON object:

[
   {
      "id":"1",
      "name":"nehil",
      "gender":"1",
      "birthday":"1991-07-22",
      "business_id":"1",
      "timestamp":"2016-03-23 04:46:42",
      "age":"24"
   },
   {
      "id":"2",
      "name":"vartika ",
      "gender":"2",
      "birthday":"1990-08-14",
      "business_id":"1",
      "timestamp":"2016-03-23 04:46:46",
       "age":"25"
   },
   {
      "id":"3",
      "name":"atharva",
      "gender":"1",
      "birthday":"1992-10-10",
      "business_id":"1",
      "timestamp":"2016-03-23 04:46:49",
       "age":"23"
   },
   {
      "id":"4",
      "name":"karan",
      "gender":"1",
      "birthday":"1992-12-22",
      "business_id":"1",
      "timestamp":"2016-03-23 04:46:52",
      "age":"23"
   }
]

On Gender select if male, I want id of all males from the object and push it in array. Later if I select min age as 23 and max age as 24, I want all males with following age to be updated in that array.

What will be best strategy to achieve this?

Following is my fiddle link - http://jsfiddle.net/Nehil/2ym3ffo0/4/

解决方案

You can use an object with special structure for searching, if you need t search for more than one item. This proposal uses an object, with this structure for filtering:

{ 
    gender: '1',
    age: {
        min: 23, 
        max: 24
    },
    birthday: function (s) { return s.match(/-10-10/); }
}

The algorithm looks for every property in search and if all comparisons are true, then the element is added to the result set.

function filter(array, search) {
    return array.filter(function (a) {
        return Object.keys(search).every(function (k) {
            return (
                a[k] === search[k] ||
                typeof search[k] === 'object' && +search[k].min <= a[k] &&  a[k] <= +search[k].max ||
                typeof search[k] === 'function' && search[k](a[k])
            );
        });
    });
}

var data = [{ id: "1", name: "nehil", gender: "1", birthday: "1991-07-22", business_id: "1", timestamp: "2016-03-23 04:46:42", age: "24" }, { id: "2", name: "vartika ", gender: "2", birthday: "1990-08-14", business_id: "1", timestamp: "2016-03-23 04:46:46", age: "25" }, { id: "3", name: "atharva", gender: "1", birthday: "1992-10-10", business_id: "1", timestamp: "2016-03-23 04:46:49", age: "23" }, { id: "4", name: "karan", gender: "1", birthday: "1992-12-22", business_id: "1", timestamp: "2016-03-23 04:46:52", age: "23" }];

document.write('<pre>' + JSON.stringify(filter(data, { birthday: function (s) { return s.match(/-10-10/); } }), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(filter(data, { gender: '2' }), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(filter(data, { gender: '1', age: { min: 23, max: 24 } }), 0, 4) + '</pre>');

这篇关于JSON对象多个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆