I am struggling with a particular dictionary in my code:
{1:['a&b','b-c','c-d'],2:['e orf ','f-k-p','g']}
My goal is to print the key and values from this dictionary. However, the code I attempted didn't yield the desired results:
for (var key in dictionary) {
// check if the property/key is defined in the object itself, not in parent
if (dictionary.hasOwnProperty(key)) {
console.log(key, dictionary[key]);
}
}
Instead of the expected output, I received some random numbers. How can I resolve this issue? This was the unexpected output I got:
key:34639values:mkey:34640values:akey:34641values:tkey:34642values:hkey:34643values:ekey:34644values:mkey:34645values:akey:34646values:tkey:34647values:ikey:34648values:ckey:34649values:skey:34650values: key:34651values:pkey:34652values:rkey:34653values:okey:34654values:bkey:34655values:lkey:34656values:ekey:34657values:mkey:34658values:
What I really want the output to look like is:
keys: 1,2
values: ['a&b','b-c','c-d'],['e orf ','f-k-p','g']
var dictionary = {1:['a','b','c'],2:['e','f','g']}
for (var key in dictionary) {
// check if the property/key is defined in the object itself, not in parent
if (dictionary.hasOwnProperty(key)) {
console.log(`key: `, key);
console.log(`values: `, dictionary[key]);
}
}
Edit: Furthermore, it turns out that the dictionary is actually being treated as string. How can I convert it back to a proper dictionary format?