【发布时间】:2020-04-09 19:14:04
【问题描述】:
问题:
我有一个对象数组。还有一个 currentObject,我目前正在查看。如果其他 2 个属性匹配,我想从来自对象数组的属性中获取值。
这里是简化的数组:
ARRAY = [{
id: 1,
metadata: {author: "Company1"}
},
{
id: 2,
metadata: {author: "Company2"}
}
这是简化的对象:
OBJECT = {
name: "Something
templateId: 2
}
所以,基本上,我想返回 metdata.author 信息,如果 ARRAY.id 匹配 OBJECT.templateId..
这是我写的代码..
const getAuthorInfo = (connTemplates: ARRAY[], currentConn: ITEM_OBJECT) => {
connTemplates.find((connTemplate: ARRAY_ITEM_OBJECT) => connTemplate.id === currentConn.templateId);
};
console.log('Author Info:', connection); // This though returns the OBJECT, not the ARRAY_ITEM
任何想法,关于如何使这项工作?我也尝试了filter,条件相同,但是当我在我的 ReactComponent 中调用它时返回了undefined。
【问题讨论】:
-
假设您的
return来自您的getAuthorInfo,您将从您的ARRAY获得一个对象。然后使用.metadata.author获取作者 -
返回对象。不是数组中的对象。所以,信息不存在。
-
您正在
connTemplates上运行.find(),这是您的阵列。您只能从您的数组中获取undefined或一个对象作为回报,我看不出您是如何获得OBJECT作为回报的?
标签: javascript arrays typescript filter find