Recursively filter array of objects

后端 未结 7 907
一个人的身影
一个人的身影 2020-11-29 00:00

Hitting a wall with this one, thought I would post it here in case some kind soul has come across a similar one. I have some data that looks something like this:

<         


        
7条回答
  •  清酒与你
    2020-11-29 00:14

    I think it will be a recursive solution. Here is one that I tried.

    function find(obj, key) {
      if (obj.value && obj.value.indexOf(key) > -1){
        return true;
      }
      if (obj.children && obj.children.length > 0){
        return obj.children.reduce(function(obj1, obj2){
          return find(obj1, key) || find(obj2, key);
        }, {}); 
      } 
      return false;
    }
    
    var output = input.filter(function(obj){
         return find(obj, 'Hit');
     });
    console.log('Result', output);
    

提交回复
热议问题