简体   繁体   中英

Get values of object using lodash

I've to use lodash for get the values of this object: http://pastebin.com/raw.php?i=U1Z8tzY0

As you can see, I have one big object. I need to get all the autonomias names with the activo property set to true ".

I've tried to use a lot of functions without result. I'm stuck.

And I can't find the way to do it properly after few days.

The following snippet should do the trick:

var res =  _(data)
             .chain()
                .result('autonomias')
                .filter({ activo: true }) 
                .pluck('name')
             .value();

Here is a demo.

You could also use native Array methods:

var res = data.autonomias.filter(function (el) {
    return el.activo === true;
}).map(function (el) {
    return el.name;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM