当前位置: 首页 > 工具软件 > Map Index > 使用案例 >

dart中list的map方法获取index

唐增
2023-12-01

dart map获取index

只需要index

list.asMap().keys.map((index) {   
    return something;
}
List<String> list = ['a', 'b', 'c', 'd'];
  
print(list.asMap()); // {0: a, 1: b, 2: c, 3: d}
print(list.asMap().keys); // (0, 1, 2, 3)
print(list.asMap().keys.map((index) => list[index]).toList());  // [a, b, c, d]

同时访问value和index

list.asMap().entries.map((entry) {
    int index = entry.key;
    String value = entry.value;
    
    return something;
}
List list = ['a', 'b', 'c', 'd'];

List ll = list.asMap().entries.map((entry) {
   int index = entry.key;
   String value = entry.value;
   return value + index.toString();
}).toList();             
print(ll); // [a0, b1, c2, d3]
 类似资料: