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

Get parent object, by nested property

I have the following object, an array with databases, each containing a collection of tables, each containing a collection of columns.

Console Output

I want to obtain the parent table, given the Id of a column and tried to avoid iterating over the collection. Just a very simple clean single line.

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

I was initially trying but quickly was challenged by the fact Tables is not an array but a collection which doesn’t support .some statements.

let table = schema.find((database,index) => database.Tables.some((column,index) => column.Id === 1234));

Is there a smart way or conversion trick that I have overlooked that prevents me from having to iterate the entire structure?

>Solution :

Use Object.values() to get an array of columns from your Tables object:

let table = schema.find(database => Object.values(database.Tables).some(column => column.Id === 1234));

Alternately, you could use Object.keys() and write it like this:

let table = schema.find(database => Object.keys(database.Tables)
                                          .some(columnName => database.Tables[columnName].Id === 1234));
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