我有以下字典:
{1:['a&b','b-c','c-d'],2:['e orf ','f-k-p','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, dictionary[key]);
}
}
我只是得到一些随机数。我该如何解决?我的输出:
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:
我希望输出为:
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]);
}
}
编辑:实际上字典被视为字符串。我怎样才能将它类型转换为字典?