I have this object: const INDOORCAMERAMODELS = { "SimpliCam": "SS001", "videodoorbell":…
- User
I have this object: const INDOOR_CAMERA_MODELS = { "SimpliCam": "SS001", "videodoorbell": "SS002", "SimpliCam": "SS003", "shield": "SSOBCM4", "dagger": "SSOBCM4", "Wireless Indoor Camera": "scout", };
- Assistant
The given object is a JavaScript object called INDOOR_CAMERA_MODELS. It contains various indoor camera models as keys and their corresponding model numbers as values. Here is the object:
const INDOOR_CAMERA_MODELS = { "SimpliCam": "SS001", "videodoorbell": "SS002", "SimpliCam": "SS003", "shield": "SSOBCM4", "dagger": "SSOBCM4", "Wireless Indoor Camera": "scout", };Please note that objects in JavaScript can't have duplicate keys. Therefore, the duplicate key "SimpliCam" will override the previous entry. In this case, the value "SS003" will be associated with the key "SimpliCam".
- User
give me a function that compares a string against the value of the each object property, and returns the property name
- Assistant
Sure! Here's a function that compares a given string against the values of each property in the
INDOOR_CAMERA_MODELSobject and returns the corresponding property name:function findPropertyName(inputValue) { for (let propertyName in INDOOR_CAMERA_MODELS) { if (INDOOR_CAMERA_MODELS[propertyName] === inputValue) { return propertyName; } } return "Property not found"; }You can call this function with a string parameter and it will iterate over each property value in the
INDOOR_CAMERA_MODELSobject. If it finds a match, it will return the corresponding property name. If no match is found, it will return "Property not found".Here's an example usage of the function:
console.log(findPropertyName("SSOBCM4")); // Output: shield (since it's the first match) console.log(findPropertyName("scout")); // Output: Wireless Indoor Camera console.log(findPropertyName("XYZ")); // Output: Property not foundNote that if multiple properties have the same value, this function will only return the first matching property name encountered during iteration.