简体   繁体   English

使用多个键在 Angular2 中过滤 map

[英]Filter a map in Angular2 with multiple keys

i have a Map as like below in a ts file.我在 ts 文件中有一个 Map,如下所示。 i need to filter the map by giving the multiple keys at a same time.我需要通过同时给出多个键来过滤 map。

tMap: Map<string, number[]> = new Map<string, number[]>();
tMap.set('T',[1,2,3]);
tMap.set('U',[4,5,6]);
tMap.set('V',[4,5,6]);

my expected result will be excluding the key "T".我的预期结果将不包括键“T”。 It should filter only U and V as a single array.它应该仅将 U 和 V 过滤为单个数组。 please advise请指教

you can create a new Map and insert filtered elements:您可以创建一个新的Map并插入过滤元素:

let newMap: Map<string, number[]> = new Map<string, number[]>();

tMap.forEach((value, key) => {
  if (key !== 'T') {
    newMap.set(key, value);
  }
});

Or just use delete() function of Map .或者只使用 Map 的delete() Map If we suppose you have a list of keys to be deleted:如果我们假设您有一个要删除的键列表:

const deletedKeys = ['A', 'E', 'T'];

deletedKeys.forEach(key => tMap.delete(key));

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

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