当前位置:网站首页>Shutter - how to copy certain elements from a map to a new map in dart/shutter?

Shutter - how to copy certain elements from a map to a new map in dart/shutter?

2022-06-24 04:55:00 nuts

flutter - How to be in Dart/Flutter Remove some elements from a Map Copy to new Map in ?

How to be in Dart / Flutter Remove some elements from a Map Copy to new Map in ?

Old_Map = {
  'A' : {Big : 'A', Small : 'a' },
  'B' : {Big : 'B', Small : 'b' },
  'C' : {Big : 'C', Small : 'c' },
  'D' : {Big : 'D', Small : 'd' },
}
​
Old_Map => New_Map

I just want to

'B' : {Big : 'B', Small : 'b' },
'C' : {Big : 'C', Small : 'c' },

The best answer

You can do this

 final oldMap = {
    'A': {'Big': 'A', 'Small': 'a'},
    'B': {'Big': 'B', 'Small': 'b'},
    'C': {'Big': 'C', 'Small': 'c'},
    'D': {'Big': 'D', 'Small': 'd'},
  };
​
  final newMap =
      Map.fromIterable(oldMap.keys.where((k) => k == 'B' || k =='C'), 
      key: (k) => k, value: (v) => oldMap[v]);

because keys return map Keyed Iterable<String>, So you can use where Method to check the required keys , Then it can be based on the old map Value filling value .

原网站

版权声明
本文为[nuts]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210831000344756z.html