-2

I have the following dictionary:

{1:['a&b','b-c','c-d'],2:['e orf ','f-k-p','g']}

I want to print the key and values from this dictionary I tried the following code:

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]);
    }
}

I am just getting some random numbers.How could I solve it? My output:

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:

I want the output as:

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: Actually the dictionary is taken as string. How can I typecast it to dictionary?

Sri Test
  • 389
  • 1
  • 4
  • 21

4 Answers4

0

I ran your code and here is the output:

enter image description here

Jayant Jeet Tomar
  • 167
  • 1
  • 1
  • 14
  • @SriTest Its working fine for everyone else. Please create a demo to show that you are getting that output? – palaѕн Jun 19 '20 at 08:45
  • @palaѕн How could I create a Demo? – Sri Test Jun 19 '20 at 08:47
  • @JayantJeetTomar I found the problem.Actually I am considering it as a dictionary but the original type is in string.I need to typecast it to dictionary for the code to work. – Sri Test Jun 19 '20 at 08:50
  • Like [this](https://meta.stackoverflow.com/a/358993/1823841) I have already added a demo in your post. You can update it. – palaѕн Jun 19 '20 at 08:50
  • Ok, then please delete the question or mention that it was an issue in your logic. – palaѕн Jun 19 '20 at 08:51
0
var dictionary = {1:['a','b','c'],2:['e','f','g']}
var keys = [];
var values = [];
for (var key in dictionary) {
    // check if the property/key is defined in the object itself, not in parent
    if (dictionary.hasOwnProperty(key)) {
        keys.push(key);
        values.push(dictionary[key]);
    }
}

console.log(`keys: `, keys);
console.log(`values: `, values);
Wiyanto Tan
  • 82
  • 1
  • 3
  • I have my special characters in the values column and when I try your code,I get the following output:```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: ``` – Sri Test Jun 19 '20 at 08:34
  • @SriTest can you create a demo to show that you are getting this output? – palaѕн Jun 19 '20 at 08:44
0

You can get object keys with

Object.keys(dictionary)

which will result with: array of two elements: ["1", "2"].

When you have key of object, you can get it's content using simple for loop:

for(let key of Object.keys(dictionary))
    console.log(dictionary[key])

result:

["a", "b", "c"]
["e", "f", "g"]

Don't forget about console.table(dictionary). For simple table structures it may help with displaying.

Rafał Figura
  • 5,428
  • 1
  • 15
  • 20
0

I have created this below utility for fulfilling your requirement.

let data = {1:['a&b','b-c','c-d'],2:['e orf ','f-k-p','g']}

const keys = Object.keys(data)
const values = Object.values(data)
console.log(`Keys: ${keys.join()}`)

let result = values.reduce((result, value) => { result.push(JSON.stringify(value)); return result}, []).join()
console.log(`Values: ${result}`)

Hope this helps.

Nithish
  • 5,393
  • 2
  • 9
  • 24