嵌套过滤器与lodash为父子关系

时间:2017-07-18 13:47:21

标签: javascript angularjs underscore.js lodash

如何创建一个lodash以获取以下多数组($ scope.myVar)的userID 10165381978及其子userID -

 {
        "children": [{
            "userId": "1",
            "name": "Kevin",
            "type": "ROOT",
            "active": true,
            "children": [{
                "userId": "10023872531",
                "name": "Selvin",
                "type": "USER",
                "active": true,
                "children": []
            }, {
                "userId": "10003200835",
                "name": "adduser",
                "type": "USER",
                "active": true,
                "children": []
            }, {
                "userId": "-1111111",
                "name": "Merisa",
                "type": "USER",
                "active": true,
                "children": []
            }, {
                "userId": "10165381976",
                "name": "Kam",
                "type": "PARENT",
                "active": true,
                "children": [{
                    "userId": "10165381977",
                    "name": "Pam",
                    "type": "USER",
                    "active": true,
                    "children": [{
                        "userId": "10165381978",
                        "name": "Ram",
                        "type": "PARENT",
                        "active": true,
                        "children": [{
                            "userId": "10232392492",
                            "name": "Sam",
                            "type": "USER",
                            "active": true,
                            "children": []
                        }]
                    }]
                }]
            }]
        }]
    }

所以从上面我想得到 -

10165381978, 10232392492

我试过了 -

var filterUser = function(userId){
     lodash.filter($scope.myVar, function(o) { return o.userId})
}

然后调用filterUser(10165381978);

但结果是空对象[]。

1 个答案:

答案 0 :(得分:2)

Lodash没有提供实现此功能的内置实用程序。您可以通过遍历遍历嵌套集合来解决此问题。检查下面的解决方案,代码的每个部分都被注释。

function getIds(collection, ids) {

  // store ids in this variable
  var result = [];
  // determines if an id is found,
  var found = false;

  // makes sure that ids is always an array of id
  ids = [].concat(ids);

  // iterate over the collection, name the callback for recursion
  // if you prefer to use lodash, then use: 
  // _.each(collection, function iterator(value) {...});
  collection.forEach(function iterator(value) {
    // Matching the list of `ids` from the iterated userId. 
    // If a match is found, then we set `found` to true.
    var isStop = ~ids.indexOf(value.userId) && (found = true);

    // did we get a match?
    if(found) {
      // add the matched ID and the IDs from its descendants
      result.push(value.userId);
    }

    // itereate recursively over its descendants
    // If you prefer to use lodash then use:
    // _.each(value.children, iterator)
    (value.children || []).forEach(iterator);

    // is the currently iterated item's ID within the list of `ids`?
    if(isStop) {
      // set `found` to false, to prevent adding IDs that aren't matched
      found = false;
    }
  });

  // return an array of IDs
  return result;

}

var data = {
  "children": [{
    "userId": "1",
    "name": "Kevin",
    "type": "ROOT",
    "active": true,
    "children": [{
      "userId": "10023872531",
      "name": "Selvin",
      "type": "USER",
      "active": true,
      "children": []
    }, {
      "userId": "10003200835",
      "name": "adduser",
      "type": "USER",
      "active": true,
      "children": []
    }, {
      "userId": "-1111111",
      "name": "Merisa",
      "type": "USER",
      "active": true,
      "children": []
    }, {
      "userId": "10165381976",
      "name": "Kam",
      "type": "PARENT",
      "active": true,
      "children": [{
        "userId": "10165381977",
        "name": "Pam",
        "type": "USER",
        "active": true,
        "children": [{
          "userId": "10165381978",
          "name": "Ram",
          "type": "PARENT",
          "active": true,
          "children": [{
            "userId": "10232392492",
            "name": "Sam",
            "type": "USER",
            "active": true,
            "children": []
          }]
        }]
      }]
    }]
  }]
};

function getIds(collection, ids) {
  
  // store ids in this variable
  var result = [];
  // determines if an id is found,
  var found = false;
  
  // makes sure that ids is always an array of id
  ids = [].concat(ids);
  
  // iterate over the collection, name the callback for recursion
  // if you prefer to use lodash, then use: 
  // _.each(collection, function iterator(value) {...});
  collection.forEach(function iterator(value) {
    // Matching the list of `ids` from the iterated userId. 
    // If a match is found, then we set `found` to true.
    var isStop = ~ids.indexOf(value.userId) && (found = true);
    
    // did we get a match?
    if(found) {
      // add the matched ID and the IDs from its descendants
      result.push(value.userId);
    }
    
    // itereate recursively over its descendants
    // If you prefer to use lodash then use:
    // _.each(value.children, iterator)
    (value.children || []).forEach(iterator);

    // is the currently iterated item's ID within the list of `ids`?
    if(isStop) {
      // set `found` to false, to prevent adding IDs that aren't matched
      found = false;
    }
  });
  
  // return an array of IDs
  return result;
  
}

console.log('ID to find: 10165381978');
console.log(getIds(data.children, '10165381978'));

console.log('IDs to find: 10023872531, 10165381976');
console.log(getIds(data.children, [
  '10023872531',
  '10165381976'
]));
.as-console-wrapper {
  min-height: 100%;
  top: 0;
}