Is it possible to “filter” a Map by value in Typescript?

谁说胖子不能爱 提交于 2019-12-05 07:18:15

It is

Array.from(map.values()).filter((item: Event) => item.event_id === eventId);

Or for TypeScript downlevelIteration option,

[...map.values()].filter((item: Event) => item.event_id === eventId);

Using lodash:

_.pickBy(thing, function(value, key) {
  return value.event_id == eventId;
});

First you need to flatten the map, Then extract the contents to an Events object

let dataSet = {
     "entry1" : {  id: "85d55e6b-f4bf-47b0" },
     "entry2" : {  visitor_id: "6665b-7555bf-978b0" } 
}

 let flattenedMap = {};
    Object.entries(dataSet).forEach(
           ([key,value]) => Object.assign(flattenedMap, value)
     );
  

console.log("The flattened Map")
console.log(flattenedMap)

let events = [];
Object.entries(flattenedMap).forEach(
    ([key, value]) => events.push({"event_id" : value})
);

console.log("The events");
console.log(events);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!