简体   繁体   English

当过滤器排除了孩子的父母时,如何在array.filter()回调函数中获得孩子的父母?

[英]How do I get a child's parents in an array.filter() callback function when they were excluded by the filter?

Today I discovered the array.filter() method and its associated callback function. 今天,我发现了array.filter()方法及其相关的回调函数。

I have an array of objects that looks like this: 我有一个看起来像这样的对象数组:

var treeAry = [
      {"id" : "200", "parent": "#", "type" : "1"},
      {"id" : "300", "parent": "#", "type" : "1"},
      {"id" : "400", "parent": "#", "type" : "1"},
      {"id" : "500", "parent": "300", "type" : "5"},
      {"id" : "600", "parent": "300", "type" : "5"},
      {"id" : "700", "parent": "400", "type" : "5"},
      {"id" : "800", "parent": "400", "type" : "5"},
      {"id" : "900", "parent": "200", "type" : "6"},
      {"id" : "1000", "parent": "200", "type" : "9"},
      {"id" : "1100", "parent": "200", "type" : "8"},
];

I was successfully able to filter for type 5: 我成功地能够过滤出类型5:

  filteredAry = [];

  filteredAry = treeAry.filter(function(val, index, ary){

    /*get all type 5*/
    return val.type === '5';

    /*get all type 5 or type 1*/
    //return val.type === '1' || val.type === '5';

  });

  console.dir(filteredAry)

Here's my question: 这是我的问题:

Although I'm able to filter for type 5, I also need Type 5's parents (a child's parent references the id , and a parent can be a #). 尽管我可以过滤类型5,但我还需要类型5的父母(孩子的parent引用id ,而parent可以是#)。 Essentially, my results should include anything with type 5, and then also record id 300 and 400 . 本质上,我的结果应包括类型5的任何内容,然后还记录id 300400 It should not include id 200 . 不应包含id 200

Should I run some sort of for each loop on filteredAry[] to find and push the corresponding item in treeAry[] ? 我是否应该对filteredAry[]每个循环运行某种形式的查找,并在treeAry[]推送相应的项目? Shouldn't I be able to use the index and ary arguments to get the parents somehow? 我不应该能够使用indexary参数以某种方式获取父母吗?

Sorry, I don't know how to do this and would love your help. 抱歉,我不知道该怎么做,希望您的帮助。 Just need a push in the right direction, Thank you! 只需要朝正确的方向推进,谢谢!

Try this: 尝试这个:

 var treeAry = [ {"id" : "200", "parent": "#", "type" : "1"}, {"id" : "300", "parent": "#", "type" : "1"}, {"id" : "400", "parent": "#", "type" : "1"}, {"id" : "500", "parent": "300", "type" : "5"}, {"id" : "600", "parent": "300", "type" : "5"}, {"id" : "700", "parent": "400", "type" : "5"}, {"id" : "800", "parent": "400", "type" : "5"}, {"id" : "900", "parent": "200", "type" : "6"}, {"id" : "1000", "parent": "200", "type" : "9"}, {"id" : "1100", "parent": "200", "type" : "8"}, ]; var result = treeAry.filter(value => { return value.type === '5' || treeAry.filter(val => val.parent === value.id && val.type === '5').length > 0; }); console.log(result) 

You can replace lambdas (arrow functions) with ordinary functions if ES6 is an issue. 如果存在ES6问题,则可以用普通函数替换lambda(箭头函数)。

One way would be 一种方法是

 var treeAry = [ {"id" : "200", "parent": "#", "type" : "1"}, {"id" : "300", "parent": "#", "type" : "1"}, {"id" : "400", "parent": "#", "type" : "1"}, {"id" : "500", "parent": "300", "type" : "5"}, {"id" : "600", "parent": "300", "type" : "5"}, {"id" : "700", "parent": "400", "type" : "5"}, {"id" : "800", "parent": "400", "type" : "5"}, {"id" : "900", "parent": "200", "type" : "6"}, {"id" : "1000", "parent": "200", "type" : "9"}, {"id" : "1100", "parent": "200", "type" : "8"}, ]; filteredAry = []; filteredAry = treeAry.filter(function(val, index, ary){ /*get all type 5*/ return val.type === '5' || ary.some(function(item){ return item.parent === val.id && item.type==='5'; }); }); console.dir(filteredAry) 

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

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