Now, let's write our retrieval function:
const getProducts = function(a,b) {
// make an efficient means of retrieval
if (products[a]) {
return products[a][b] || null
}
return null
}
If we look at this logic, first we ensure the first key exists. If it exists, we return either x.y or null if y does not exist. Objects are picky in that if you try to refer to a value of a key that doesn't exist, you'll get an error. Thus, we first need to existence-check our key. If the key exists and the key/value pair exists, return the computed value; otherwise, we return null. Notice the return products[a][b] || null short-circuit: this is an efficient way of saying "return the value or something else." If products[a][b] does not exist, it will respond with a falsy value, and the OR operation will take over. Efficient!
Take a look at the solution code for the answer to the bonus question. The same principles of existence...