Creating Nested Objects on the Fly With Javascript
<p>I'm new to Javascript coming from a Python background, where it's easy to created nested data using custom dictionaries and <code>.get</code> methods. What I'm trying to do is created a nested object of artist data that takes on this form: <code>artistDict[artist][albumName] = albumYear</code>. I need to create this object on the fly by iterating over an iterable of album objects. Here's the code I'm currently using:</p>

<pre><code>albumDict = {};
albums.forEach(function(item){
albumDict[item.artist][item.name] = item.year;
});
document.write(albumDict);
</code></pre>

<p>This doesn't work, which isn't surprising, since something like this wouldn't work in Python either. However, in Python I could use a <code>.get</code> method to check if an entry was in the dictionary and create it if not -- is there something similar, or any other utility that I could use to achieve my goal in JS? </p>
