【发布时间】:2018-03-18 05:46:03
【问题描述】:
我正在寻找一种按值而不是键过滤地图的方法。我有一个在我的 Angular 应用程序中建模如下的数据集:
{
"85d55e6b-f4bf-47bb-a988-78fdb9650ef0": {
is_deleted: false,
public_email: "007@example.org",
id: "85d55e6b-f4bf-47bb-a988-78fdb9650ef0",
modified_at: "2017-09-26T15:35:06.853492Z",
social_url: "https://facebook.com/jamesbond007",
event_id: "213b01de-da9e-4d19-8e9c-c0dae63e019c",
visitor_id: "c3c232ff-1381-4776-a7f2-46c177ecde1c",
},
}
这些条目上的键与条目值上的id 字段相同。
鉴于其中几个条目,我想过滤并返回一个 new Map(),其中仅包含具有给定 event_id 的那些条目。如果这是一个数组,我会执行以下操作:
function example(eventId: string): Event[] {
return array.filter((item: Event) => item.event_id === eventId);
}
本质上,我正在尝试复制Array.prototype.map() 的功能 - 只是在地图而不是数组上。
如果 Lodash 有助于以更简洁的方式实现这一点,我愿意使用它,因为它已经在我的项目中可用。
【问题讨论】:
-
您要过滤的
Map在哪里?我看到了Object。 They're not the same thing.
标签: angular typescript ecmascript-6 lodash