coool
coool

Reputation: 8297

In Underscore how to use each on a nested json object

I have a json object

user = { name: "somename", personal : { age:"19",color:"dark"}}
_.each(user,function(value){ if(isNaN(value){console.log(value)} )

How to get to the nested object value.

Upvotes: 0

Views: 6604

Answers (1)

SMathew
SMathew

Reputation: 4003

Something like this will work. If you expect even more deeply nested objects, then create a recursive function. _.isObject(val) is the key here.

_.each( {name: "somename", personal : { age:"19",color:"dark"}}, function(val) {
    if (_.isObject(val)) {
       _.each(val, function(v) {
            console.log(v)
       }) 
    }
})

Upvotes: 1

Related Questions