Search recursively for value in object by property name
<p>I'm building an utility function that should search for a property name and return its value once it is found. It should do this recursively:</p>

<pre><code>// Function
util.findVal = (object, propName) =&gt; {
  for (let key in object) {
    if (key === propName) {
      console.log(propName)
      console.log(object[key])
      return object[key]
    } else {
      util.findVal(object[key], propName)
    }
  }
}

// Input
object: {
  photo: {
    progress: 20
  }
}

// Usage
util.findVal(object, 'progress')
</code></pre>

<p>However the console log goes forever and the browser crashes. What am I doing wrong?</p>

<p><strong>EDIT:</strong></p>

<p>This is how I'm calling the function:</p>

<pre><code>// Input

item: {
  photo: {
    file: {},
    progress: 20
  }
}

this.findProgress(item)

methods: {
  findProgress (item) {
    return util.findVal(item, this.propName)
  }
}
</code></pre>