javascript and dict getkeys


    Sep 07 2019 | 5:21 pm
    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?

    • Sep 08 2019 | 11:36 pm
      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 :)
    • Sep 09 2019 | 11:39 am
      thanks that helps (stylistically awkward when all you want to do is a for loop but heyho)
    • Jun 10 2020 | 1:34 pm
      concur!