简体   繁体   English

通过嵌套属性获取父对象

[英]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.我有以下对象,一个带有数据库的数组,每个包含一个表集合,每个表包含一个列集合。

控制台输出

I want to obtain the parent table, given the Id of a column and tried to avoid iterating over the collection.我想获取父表,给定列的 Id 并试图避免迭代集合。 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.我最初正在尝试,但很快就受到了事实的挑战 Tables 不是数组而是一个不支持 .some 语句的集合。

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?是否有我忽略的智能方法或转换技巧可以防止我不得不迭代整个结构?

Use Object.values() to get an array of columns from your Tables object:使用Object.values()从 Tables 对象中获取列数组:

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:或者,您可以使用Object.keys()并像这样编写:

let table = schema.find(database => Object.keys(database.Tables)
                                          .some(columnName => database.Tables[columnName].Id === 1234));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM