如何反转Map的键和值?例如,{1:a , 2:b , 3:c} => {a:1 ,b:2 ,c:3}
{1:a , 2:b , 3:c} => {a:1 ,b:2 ,c:3}
tzdcorbm1#
试试下面这段代码:
Map<int, String> map = {1: "a", 2: "b", 3: "c"}; Iterable<String> values = map.values; Iterable<int> keys = map.keys; Map<String, int> reversedMap = Map.fromIterables(values, keys); print(reversedMap); // {a:1 ,b:2 ,c:3}
jljoyd4f2#
您可以执行以下操作:
const items = {1:'a' , 2:'b' , 3:'c'}; void main() { final inverted = items.map((key, value) => MapEntry(value, key)); print(inverted); }
它记录
{a: 1, b: 2, c: 3}
2条答案
按热度按时间tzdcorbm1#
试试下面这段代码:
jljoyd4f2#
您可以执行以下操作:
它记录