title: Javascript - Deeply Filtering The Array
body: <p>I have an object which looks like this for some reason:</p>
  
  <pre><code>const obj = [{
      slug: 'something',
      nestedSlug: [{
        slug: 'n1',
        deeplyNestedSlug: [{
            slug: 'n11'
          },
          {
            slug: 'n21'
          }
        ]
      }]
    },
    {
      slug: 'nothing',
      nestedSlug: [{
        slug: 'n2',
        deeplyNestedSlug: [{
            slug: 'n12'
          },
          {
            slug: 'n22'
          }
        ]
      }]
    },
    {
      slug: 'anything',
      nestedSlug: [{
        slug: 'n3',
        deeplyNestedSlug: [{
            slug: 'n13'
          },
          {
            slug: 'n23'
          }
        ]
      }]
    }
  ]
  </code></pre>
  
  <p>I want the result something like this: <code>['n1', 'n22', 'n3']</code>. And I tried the following:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const requiredItems = ['n1', 'n22', 'n3']
  let collection = []
  const obj = [{
      slug: 'something',
      nestedSlug: [{
        slug: 'n1',
        deeplyNestedSlug: [{
            slug: 'n11'
          },
          {
            slug: 'n21'
          }
        ]
      }]
    },
    {
      slug: 'nothing',
      nestedSlug: [{
        slug: 'n2',
        deeplyNestedSlug: [{
            slug: 'n12'
          },
          {
            slug: 'n22'
          }
        ]
      }]
    },
    {
      slug: 'anything',
      nestedSlug: [{
        slug: 'n3',
        deeplyNestedSlug: [{
            slug: 'n13'
          },
          {
            slug: 'n23'
          }
        ]
      }]
    }
  ]
  
  let parentIndex,
    childIndex
  
  requiredItems.map(x =&gt; {
    const grandParent = obj.nestedSlug.findIndex((y, i) =&gt; {
      if (y.slug === x) {
        parentIndex = i
        return true
      } else {
        return false
      }
    })
  
    if (grandParent !== -1) {
      collection.push(obj.nestedSlug[parentIndex])
    } else {
      const children = obj.nestedSlug[i].deeplyNestedSlug.findIndex((z, i) =&gt; {
        if (z.slug === x) {
          childIndex = i
          return true
        } else {
          return false
        }
      })
  
      if (children !== -1) {
        collection.push(obj.nestedSlug[parentIndex].deeplyNestedSlug[childIndex])
      }
    }
  })
  
  console.log(collection)</code></pre>
  </div>
  </div>
  </p>
  
  <p>...which, of course didn't work at all. Please help!</p>