Skip to content

Commit e0eb503

Browse files
committed
Add Dart Map .map() term documentation
This pull request adds a new term entry for the Dart Map.map() method. The entry explains how .map() transforms each key-value pair of a map into a new map using a provided function. It includes a clear syntax section and a simple example demonstrating the method in use, following Codecademy Docs content standards and writing guidelines. This addition helps learners better understand how to work with Dart maps in a clear and beginner-friendly way.
1 parent 23d0583 commit e0eb503

File tree

1 file changed

+87
-0
lines changed
  • content/dart/concepts/map/terms/map

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
Title: 'Map.map()'
3+
Description: 'Map.map() transforms each key-value pair in a Dart Map into a new Map.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Web Development'
7+
Tags:
8+
- 'Dart'
9+
- 'Data Structures'
10+
- 'Methods'
11+
CatalogContent:
12+
- 'learn-dart'
13+
- 'paths/computer-science'
14+
---
15+
16+
In Dart, the `.map()` method is used to create a new map by transforming each key-value pair of an existing map using a provided function.
17+
18+
## Syntax
19+
20+
```pseudo
21+
myMap.map((key, value) => MapEntry(newKey, newValue))
22+
```
23+
24+
- `myMap`: The map whose entries are to be transformed.
25+
- `key`: The key from the original map.
26+
- `value`: The value associated with the key.
27+
- `MapEntry`: Represents a single key-value pair in the new map.
28+
29+
## Example 1
30+
31+
In the following example, the `.map()` method is used to create a new map by increasing each value by 1:
32+
33+
```dart
34+
void main() {
35+
Map<String, int> myMap = {
36+
'a': 1,
37+
'b': 2,
38+
'c': 3
39+
};
40+
41+
print('Original Map: $myMap');
42+
43+
// Using .map() to increase each value by 1
44+
Map<String, int> newMap = myMap.map(
45+
(key, value) => MapEntry(key, value + 1)
46+
);
47+
48+
print('New Map after using .map(): $newMap');
49+
}
50+
```
51+
52+
Here is the output for the above example:
53+
54+
```shell
55+
Original Map: {a: 1, b: 2, c: 3}
56+
New Map after using .map(): {a: 2, b: 3, c: 4}
57+
```
58+
59+
## Example 2
60+
61+
In the following example, the `.map()` method is used to convert all values in a map to uppercase:
62+
63+
```dart
64+
void main() {
65+
Map<String, String> myMap = {
66+
'x': 'apple',
67+
'y': 'banana',
68+
'z': 'cherry'
69+
};
70+
71+
print('Original Map: $myMap');
72+
73+
// Using .map() to convert values to uppercase
74+
Map<String, String> newMap = myMap.map(
75+
(key, value) => MapEntry(key, value.toUpperCase())
76+
);
77+
78+
print('New Map after using .map(): $newMap');
79+
}
80+
```
81+
82+
Here is the output for the above example:
83+
84+
```shell
85+
Original Map: {x: apple, y: banana, z: cherry}
86+
New Map after using .map(): {x: APPLE, y: BANANA, z: CHERRY}
87+
```

0 commit comments

Comments
 (0)