Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions content/dart/concepts/map/terms/map/map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
Title: '.map()'
Description: '.map() transforms each key-value pair in a Dart Map into a new Map.'
Subjects:
- 'Computer Science'
- 'Web Development'
Tags:
- 'Dart'
- 'Data Structures'
- 'Map'
CatalogContent:
- 'learn-dart'
- 'paths/computer-science'
---

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.

## Syntax

```pseudo
myMap.map((key, value) => MapEntry(newKey, newValue))
```

- `myMap`: The map whose entries are to be transformed.
- `key`: The key from the original map.
- `value`: The value associated with the key.
- `MapEntry`: Represents a single key-value pair in the new map.

## Example 1

In the following example, the `.map()` method is used to create a new map by increasing each value by 1:

```dart
void main() {
Map<String, int> myMap = {
'a': 1,
'b': 2,
'c': 3
};

print('Original Map: $myMap');

// Using .map() to increase each value by 1
Map<String, int> newMap = myMap.map(
(key, value) => MapEntry(key, value + 1)
);

print('New Map after using .map(): $newMap');
}
```

Here is the output for the above example:

```shell
Original Map: {a: 1, b: 2, c: 3}
New Map after using .map(): {a: 2, b: 3, c: 4}
```

## Example 2

In the following example, the `.map()` method is used to convert all values in a map to uppercase:

```dart
void main() {
Map<String, String> myMap = {
'x': 'apple',
'y': 'banana',
'z': 'cherry'
};

print('Original Map: $myMap');

// Using .map() to convert values to uppercase
Map<String, String> newMap = myMap.map(
(key, value) => MapEntry(key, value.toUpperCase())
);

print('New Map after using .map(): $newMap');
}
```

Here is the output for the above example:

```shell
Original Map: {x: apple, y: banana, z: cherry}
New Map after using .map(): {x: APPLE, y: BANANA, z: CHERRY}
```