All collection objects contain a map
method that takes a Function
as an argument, which must take a single argument. This returns an Iterable
backed by the collection. When the Iterable
is iterated, each step calls the function with a new element of the collection, and the result of the call becomes the next element of the iteration.
You can turn an Iterable
into a collection again by using the Iterable.toSet()
or Iterable.toList()
methods, or by using a collection constructor which takes an iterable like Queue.from
or List.from
.
Example:
main() {
var cats = [
'Abyssinian',
'Scottish Fold',
'Domestic Shorthair'
];
print(cats); // [Abyssinian, Scottish Fold, Domestic Shorthair]
var catsInReverse =
cats.map((String cat) {
return new String.fromCharCodes(cat.codeUnits.reversed);
})
.toList(); // [nainissybA, dloF hsittocS, riahtrohS citsemoD]
print(catsInReverse);
}
See dartpad example here: https://dartpad.dartlang.org/a18367ff767f172b34ff03c7008a6fa1