Dict object with single element and getkeys() method in JS
Hi all,
I started experimenting with JS object in a M4L device.
It seems that the method getkeys()
on a Dict
object with a single element returns the single items of the key present in the dictionary.
E.g.
Given the dictionary
d = {"foo": "bar"}
When calling d.getkeys()
the output is ["f", "o", "o"].
I was expecting to get ["foo"]
.
Am I doing anything wrong or is this a bug?
Hi! Thank you for checking in on your findings.
This is indeed a known bug for dictionaries of a single element.
Is there any workaround for that?
While the bug is in the queue, here's what you could try in the meantime...
My first thought was that if you could check the length of the dict before doing the getkeys() call on it, you could do error handling around the case when the length is 1. I don't know of a good way to do this other than using the getkeys() call itself, so that wouldn't work.
However, one pretty hacky idea: You could insert two dummy key-value pairs into the dict before doing a getkeys() call on it. You would have to make sure you're not inserting any keys that already exist (otherwise you'd overwrite the existing values' data). Then, from the returned array, remove the two keys you inserted earlier. This would guarantee you wouldn't ever have a dict with fewer than two keys.