Checking existence of nested property in an object javascript
<p>I have already reviewed some of the answers to similar questions, however, I want to ask my question differently.</p>
<p>Let's say we have a string like <strong>&quot;level1.level2.level3. ...&quot;</strong> that indicates a nested property in an object called <strong>Obj</strong>.</p>
<p>The point is that we <em>may not</em> know how many nested properties exist in this string. For instance, it may be &quot;level1.level2&quot; or &quot;level1.level2.level3.level4&quot;.</p>
<p>Now, I want to write a function, that given the <strong>Obj</strong> and the string of properties as input, to simply tell us if such a nested property exists in the object or not (let's say true or false as output).</p>
<hr />
<p>Update:
Thanks to @Silvinus, I found the solution with a minor modification:</p>
<pre><code>        private checkNestedProperty(obj, props) {
        var splitted = props.split('.');
        var temp = obj;
        for (var index in splitted) {
            if (temp[splitted[index]] === 'undefined' || !temp[splitted[index]]) return false;
            temp = temp[splitted[index]];
        }
        return true;
    }
</code></pre>