0

I have a JavaScript object, its shape is like this:

const obj = {
              "User": ["u1", "u2"],
              "Role": ["r1", "r2"],
              "Company": ["c1", "c2", "c3"]
}

How do I iterate over this object, so that I can print out:

User:
 u1
 u2

Role:
 r1
 r2

Company:
 c1
 c2
 c3

Thanks.

Olivia
  • 195
  • 4
  • 14

1 Answers1

1

You can try the following code, I am iterating over the object and for each key I am printing out the corresponding array values.

const obj = {
              "User": ["u1", "u2"],
              "Role": ["r1", "r2"],
              "Company": ["c1", "c2", "c3"]
             }

Object.keys(obj).forEach((prop)=> {console.log(prop+':\n'+ obj[prop].join('\n'))});
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44