Accessing a property like desk.height is great when the name of the prop is static, meaning you know what it is before runtime. But what if the key is dynamic? Like, what if the user enters a string and you need to use that as the lookup key?
Bracket notation solves this.
const desk = {
wood: "maple",
width: 100,
};
console.log(desk.wood);
// prints "maple"
console.log(desk["wood"]);
// also prints "maple"
const key = "wood";
console.log(desk[key]);
// also prints "maple"
For example, maybe the key is passed in as a parameter to a function:
function getLastName(users, firstName) {
return users[firstName];
}
Complete the getProviderCount function. It takes two parameters:
provider (string): The name of the cellular provider (e.g., 'Verizon', 'AT&T').counts (object): An object where each key is a provider name, and the value is the number of phone numbers associated with that provider.The function should return the count of phone numbers for the given provider. If the provider doesn't exist in the counts object, return 0.