[英]Angular LoadAsh - filtering records matching by fields on Multiple Objects of different Type
I've been struggling a bit learning loadash and how to correctly pull the data I want with some more advanced tricks. 我一直在努力学习loadash,以及如何使用一些更高级的技巧正确提取所需的数据。 Single objects and lookups are pretty simple but i'm trying to pull all array records by a groupId, if that groupId exists in another object that isn't the same.
单个对象和查找非常简单,但是如果该groupId存在于另一个不同的对象中,则我试图通过groupId提取所有数组记录。
For example: Generic JSON example of objects, each are arrays of records. 例如:对象的通用JSON示例,每个对象都是记录数组。
Groups ..
{
groupId:
name:
code:
}
Options ..
{
groupId:
optionId:
name:
code:
}
The problem I'm having is pulling all Options only if that groupId exist in the Groups array in loadash. 我遇到的问题是,仅当loadash的Groups数组中存在该groupId时,才拉所有选项。
I've tried some stuff like 我尝试过一些类似的东西
var results = [];
_.forEach(Groups, function(g) {
var found _.find(Options, g, function(option, group) {
return option.groupId === group.groupId;
})
results.push(found);
});
I haven't had much luck figuring out the best way to filter these down. 我没有太多的运气找出最好的方法来过滤掉它们。
Any words if wisdom would be appreciated, thanks! 如果有什么话可以感激,谢谢!
Something like this should work, 这样的事情应该起作用,
var result = _.filter(Options, function(o) {
return _.filter(Groups, function(g) { return g.groupId == o.groupid; }).length > 0;
});
actually i think the inner search would perform better with find
, since it returns the first match, not sure though 实际上我认为内部搜索使用
find
会更好,因为它返回第一个匹配项,虽然不确定
var result = _.filter(Options, function(o) {
return _.find(Groups, { 'groupId': o.groupid });
});
hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.