5

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/

2 Answers 2

3

You can use 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;
        })

    }

Working fiddle;

Similar you can do for age with Min and Max condition

Sign up to request clarification or add additional context in comments.

1 Comment

intGender will be intGenderNum in function right?
0

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>');

5 Comments

If I want people with birthday today, then how can I filter birthday with today's date and month and neglecting year? eg. today's date is 10-10-2016 and I want people with birthday today like 1992-10-10.
you can extend the object to accept a function for getting part result.
I tried to get list of people who's birthday is from '/-04-10/' to '/-10-10/' and I am getting null value. Updated Fiddle for what I tried : jsfiddle.net/Nehil/2ym3ffo0/5
you need a function for birthday like this: function (s) { return s.match(/\d{4}-(10|0[456789])-10/); }
The above function gives only 1 data set instead of 2 and If I try giving range of 7 days I get value null. Like date from /-07-17/ to /-07-23/? Can You edit the answer above? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.