I want to populate a dictionary of dictionaries in JavaScript.
As soon as I have a resident's address and name, I want to write it into the object, i.e.
addressCache[city][street][number] = name
However, if my dictionary doesn't contain a value for the city's key yet, it's accessing a street of undefined since the dictionary does not exist yet.
My workaround at the moment is
adressCache = {}
function setAddressEntry(city, street, number, familyName) {
if(!adressCache[city]) {
adressCache[city] = {}
}
if(!adressCache[city][street]) {
adressCache[city][street] = {}
}
adressCache[city][street][number] = familyName
}
I don't think that's the right way of doing that. How can I improve this code? Or is it common to write a helper function for that?