I want to create an associative array:
var aa = {} // Equivalent to Object(), new Object(), etc...
And I want to be sure that any key I access is going to be a number:
aa['hey'] = 4.3; aa['btar'] = 43.1;
I know JavaScript doesn’t have typing, so I can’t automatically check this, but I can ensure in my own code that I only assign strings to this aa.
Now I’m taking keys from the user. I want to display the value for that key. However, if the user gives me something like "toString", the user gets back a function, not an int! Is there a way to make sure any string the user gives me is only something I define?
Is the only solution something like the following?
delete aa['toString']; delete aa['hasOwnProperty'];
etc…
This may work for you:
Alternatively, I recommend this generic solution:
Note the following: The key will internally be converted to a string because the key is actually a name of an attribute.
If trying to use an object:
Now that you know that it is always a string, let’s use it: