Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to loop over an object inside of an object

The server gives the following response:

SomeObject:{
    Object1:{
        id: 123456789,
        name: "Foo"
    },
    Object2:{
        id: 123456789,
        name: "Bar"
    }
}

Is it possible to loop over SomeObject and display both the id and name of Object1/Object2? Searching for this mostly lead to using Object.keys(SomeObject).map however those are use to get the string of Object1/Object2.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const SomeObject = { Object1: { id: 123456789, name: "Foo" }, Object2:{ id: 123456789, name: "Bar" } };

const res = 
  Object.values(SomeObject)
  .forEach(({ id, name }) => console.log(id, name));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading