Multiple JSON Object Filter

I have a JSON object in which data receives a filter when selecting elements from a form. My form has the following elements:

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

The 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 Genderselect if a man, I want idall the males from the object and click it in the array. Later, if I choose the minimum age of 23 years and the age of up to 24 years, I want all men with the next age to be updated in this array.

What would be the best strategy to achieve this?

Below is a link to my fiddle - http://jsfiddle.net/Nehil/2ym3ffo0/4/

+4
source share
2 answers

filter

var arrMale,arrFeMale ; 
arrMale = geGenderData(1)
arrFemale = geGenderData(2)

console.log(arrMale)
console.log(arrFemale)

    function geGenderData(intGenderNum){

        return data.filter(function(oneObj,key){

           return oneObj.gender ==intGender;
        })

    }

fiddle;

Min Max

+3

, . :

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

search, , .

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>');
Hide result
0

All Articles