简体   繁体   English

使用lodash获取对象的值

[英]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 我必须使用lodash来获取此对象的值: 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 ". 我需要将activo属性设置为true来获取所有autonomias名称。

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: 您还可以使用本机Array方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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