简体   繁体   中英

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. 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.

For example: Generic JSON example of objects, each are arrays of records.

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.

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

var result = _.filter(Options, function(o) { 
  return _.find(Groups, { 'groupId': o.groupid });
});

hope this helps.

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