title: Recursively filter array of objects
body: <p>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:</p>
  
  <pre><code>const input = [
    {
      value: 'Miss1',
      children: [
        { value: 'Miss2' },
        { value: 'Hit1', children: [ { value: 'Miss3' } ] }
      ]
    },
    {
      value: 'Miss4',
      children: [
        { value: 'Miss5' },
        { value: 'Miss6', children: [ { value: 'Hit2' } ] }
      ]
    },
    {
      value: 'Miss7',
      children: [
        { value: 'Miss8' },
        { value: 'Miss9', children: [ { value: 'Miss10' } ] }
      ]
    },
    {
      value: 'Hit3',
      children: [
        { value: 'Miss11' },
        { value: 'Miss12', children: [ { value: 'Miss13' } ] }
      ]
    },
    {
      value: 'Miss14',
      children: [
        { value: 'Hit4' },
        { value: 'Miss15', children: [ { value: 'Miss16' } ] }
      ]
    },
  ];
  </code></pre>
  
  <p>I don't know at run time how deep the hierarchy will be, i.e. how many levels of objects will have a children array. I have simplified the example somewhat, I will actually need to match the value properties against an array of search terms. Let's for the moment assume that I am matching where <code>value.includes('Hit')</code>. </p>
  
  <p>I need a function that returns a new array, such that:</p>
  
  <ul>
  <li><p>Every non-matching object with no children, or no matches in children hierarchy, should not exist in output object</p></li>
  <li><p>Every object with a descendant that contains a matching object, should remain</p></li>
  <li><p>All descendants of matching objects should remain</p></li>
  </ul>
  
  <p>I am considering a 'matching object' to be one with a <code>value</code> property that contains the string <code>Hit</code> in this case, and vice versa. </p>
  
  <p>The output should look something like the following: </p>
  
  <pre><code>const expected = [
    {
      value: 'Miss1',
      children: [
        { value: 'Hit1', children: [ { value: 'Miss3' } ] }
      ]
    },
    {
      value: 'Miss4',
      children: [
        { value: 'Miss6', children: [ { value: 'Hit2' } ] }
      ]
    },
    {
      value: 'Hit3',
      children: [
        { value: 'Miss11' },
        { value: 'Miss12', children: [ { value: 'Miss13' } ] }
      ]
    },
    {
      value: 'Miss14',
      children: [
        { value: 'Hit4' },
      ]
    }
  ];
  </code></pre>
  
  <p>Many thanks to anyone who took the time to read this far, will post my solution if I get there first.</p>