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.

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

Leave a Reply