Enumerate or map through a list with index and value in Dart

后端 未结 8 2152
难免孤独
难免孤独 2020-12-08 01:28

In dart there any equivalent to the common:

enumerate(List) -> Iterator((index, value) => f)
or 
List.enumerate()  -> Iterator((index, value) =>          


        
8条回答
  •  眼角桃花
    2020-12-08 02:01

    Use asMap to convert List to map first. The index of element is the key. The element becomes value. Use entries to map the key and value to anything you want.

    List rawList = ["a", "b", "c"];
    List argList = rawList.asMap().entries.map((e) => '${e.key}:${e.value}').toList();
    print(argList);
    

    Output:

    [0:a, 1:b, 2:c]
    

提交回复
热议问题