A_user
A_user

Reputation: 2157

how to filter object in javascript

I an working in javascript and stuck in understanding the objects. Here is my scenario.

I have an object which in turn has multiple objects in it like.

data {
"aa" : object
"bb" : object
"cc" : object
}

//expanding aa bb and cc

aa {
name : "a"
type : "static"
value : "123"
}

bb {
name : "b"
type : "dyn"
value : "343"
}

cc {
name : "c"
type : "dyn"
value : "545"
}

Now what I want to achieve is that i have an object which should have those objects that have type = "dyn" so i want to have a reqdata object like this

reqdata {
"bb" : object
"cc" : object
}

I have written a code to do this but it is not working as my reqdata has all the data.

   var reqData = $.each (data, function(key, d){
        if (type === "dyn")
            return d;                             
        });

Can any one guide me what the proper and efficient way of looping through the object.

Thanks any help and guidance will be appreciated

Upvotes: 0

Views: 151

Answers (3)

Erik  Reppen
Erik Reppen

Reputation: 4635

Shorter.

$(data).each( function(){
    if(this.type === 'dyn'){ doStuff(this); }
} );

Now, IMO, constructor name is closer to type in JS. I'd build those objects with a function constructor names 'Dyn' and check <instance>.constructor.name for 'Dyn' but you would have to normalize for IE<=8 which involves parsing <instance>constructor.toString() so perhaps more trouble than it's worth.

But if you want to understand JS objects. Ditch the jQuery until you do. Learn how to use:

for(var x in object){
    console.log('propertyLabel:' +x+', property:' + object[x]+'\n');
}

Then go back to understanding how jQuery itself works. Lots of juicy stuff under the hood there.

Upvotes: 0

slebetman
slebetman

Reputation: 113886

If you're used to functional programming, you can write your own filter function for objects:

function oFilter (obj, f) {
    var result = {};
    for (var x in obj) {
        if (
            obj.hasOwnProperty(x) &&
            f(x,obj[x])
        ) {
            result[x] = obj[x];
        }
    }
    return result;
}

Then it'd be as you expected:

var reqData = oFilter(data, function(key,d){
    if (d.type === "dyn") return true;
    return false;
});

Similarly for map:

function oMap (obj, f) {
    var result = {};
    for (var x in obj) {
        if (obj.hasOwnProperty(x)) {
            result[x] = f(x,obj[x]);
        }
    }
    return result;
}

Reduce doesn't make sense for objects though.

Upvotes: 0

I Hate Lazy
I Hate Lazy

Reputation: 48761

You need to create a new object, test the type property, and assign the current sub-object to the new one if the type is what you want.

          // v--- Holds the results
var newObj = {};
                       //   v--- The current sub-object
$.each(data, function(key, obj){
    if (obj.type === "dyn") // <-- Test its `type` property
        newObj[key] = obj;  // <--   and assign to `newObj` if it matches
});

You should note that you're not making a copy of obj when you assign it to newObj. You're making a copy of a reference to obj.

This means that data and newObj share the same objects. Changes made via data are observable from newObj, and vice versa.

Upvotes: 1

Related Questions