Check if an array contains a specified object
<p>The following function searches an object recursively through an object that has nested arrays:</p>

<pre><code>function findDeep(arr, obj) {
  console.log(arr)
  if (arr.indexOf(obj) !== -1) {
    console.log(arr)
    return arr
  } else {
    arr.forEach(item =&gt; {
      if (item.children) findDeep(item.children, obj)
    })
  }
}

const colors = {
  children: [
    {
      name: 'white',
    },
    {
      name: 'yellow',
      children: [
        {
          name: 'black'
        }
      ]
    }
  ]
}

const color = {
  name: 'black'
}

findDeep(colors.children, color)
</code></pre>

<p>The first <code>console.log(arr)</code> do log the matched array:</p>

<pre><code>[
  { name: 'black' }
]
</code></pre>

<p>But he second <code>console.log(arr)</code> doesn't log anything. Shouldn't <code>arr.indexOf(obj)</code> return 1, and therefore make the second <code>console.log(arr)</code> log the array?</p>

<p>Here's the <a href="https://codepen.io/alexcheninfo/pen/ZWopGq" rel="nofollow">CodePen</a>.</p>