1

I have an array like this

var userdata = [
    {"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active"},
    {"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active"},
    {"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active"},
    {"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Inactive"}
]

I need to filter this array on some conditions. The form of these conditions are like this.

var search_object = {gender:"M",city:"Seattle, WA"}
// Gender = M and city =  'Seattle, WA'
var search_object1 = {gender:"M"}
var search_object2 = {city:"Seattle, WA"}
// This is same as above
var search_array = {status:["Active","Inactive"]}
// Status Active or Inactive
var search_array = [{status:"Active"},{status:"Inactive"}]
// Same as above
var search_object1 = {gender:"F"}
var search_array   = [{status:"Active"},{status:"Inactive"}]
//Gender = F and status = Active or Inactive
var search_object = {gender:"F"}
var search_array  = [{status:["Active","Inactive"]}]
// same as above

I have tried looping but failed. Please help or suggest or provide some proper links to get help.

5
  • 2
    Please post your loop. Commented May 21, 2014 at 7:08
  • i mean to say i am not javascript expert and failed to provide and and or to filter items Commented May 21, 2014 at 7:09
  • 1
    Seems like a job for the function appropriately named filter Commented May 21, 2014 at 7:17
  • You have to loop the array! Also the last two search_object are no valid Javascript objects. Commented May 21, 2014 at 7:18
  • thanks i think i mistyped let me edit Commented May 21, 2014 at 7:19

2 Answers 2

7

The following code covers all the cases you mentioned.

function search(searchObj, data) {
    if(searchObj instanceof Array) {
        return data.reduce(function(prev, current, index, array) {
             return prev.concat(search(current, data));
        }, []);
    } else {
        var results = data.filter(function(el) {
             for(var prop in searchObj) {
                if(searchObj[prop] instanceof Array) {
                    if(searchObj[prop].indexOf(el[prop]) == -1) {
                        return false;
                    }
                } else
                if(el[prop] !== searchObj[prop]) {
                    return false;
                }
            }

            return true;
        });

        return results;
    }
};

search(search_object, userdata);

Here is the working example in JSFiddle.

And here are some links to the functions I've used above:

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

7 Comments

this returns empty result
Okay, as per your update I've updated my solution too. Now it covers all the cases. Check it out.
thanks i have tested some cases and they are working fine. I will test others when find some time
Hi! do you think you have done this condition too. Gender = F and status = Active or Inactive. these are surely two separate parameters. How can i combine them in to one to use it
+1 Very nice answer! I'm using this now for searching complex objects :) Refactored it for less bytes..
|
1

Just what RGraham said in the comments, you can use the filter function on arrays.

var search_object = {gender:"M",city:"Seattle, WA"};
var filtered = userdata.filter(function(obj){
    return (obj.gender === search_object && obj.city === search_object.city)
});
filtered[0];//Array with objects that return true;

1 Comment

how can i add obj.dynamic_column coming from the condition not obj.gender

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.