user87267867
user87267867

Reputation: 1419

Pattern match inside object in nodejs

I have a object like this

var obj = {"$match":{"name.first":"aa"}}

In nodejs, how can I search for $ pattern inside an object. Any help on this will be really helpful.

Thanks.

Upvotes: 0

Views: 1957

Answers (2)

user87267867
user87267867

Reputation: 1419

var text = JSON.stringify(obj); var n=text.match(/\$/g);

Upvotes: 1

OneOfOne
OneOfOne

Reputation: 99332

Short version? you can't, long version? you have to write your own function to do it.\

a very simple implementation :

var searchObj = function(obj, key, value) {
    if(obj[key] === value) return obj;
    for(var k in obj) {
        var v = obj[k];
        if(obj.hasOwnProperty(k) && typeof v === 'object'){
            if(v[key] === value) return v;
        }
    }
}

Upvotes: 0

Related Questions