javascript and dict getkeys
fairly new to Javascript so this may be a simple one to fix but:
I am having an issue with the getkeys() method for dictionaries in Javascript.
if I have a dictionary with 2
e.g.
{
"dog" : 1,
"cat" : 2
}
getkeys returns 2 elements and keys.length is 2. All well and good
if I only have one key
{
"dog" : 1,
}
I get an array of 3 elements 'd' 'o' and 'g'
and keys.length comes back as 3
I understand that dog is really an array of three characters but this is quite frustrating - any thoughts?
You can typecheck against getting returned a string or number using (typeof === ‘string’ || typeof === ‘number’) and force the value into an array. (Obviously checking for number shouldn’t matter on getkeys since thats an invalid key, but having a library call which checks both is useful)
But yes it would be nice if getkeys() always returned a consistent datatype :)
thanks that helps
(stylistically awkward when all you want to do is a for loop but heyho)
concur!