-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add Dart Map .map() term documentation #8122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
omkarhole
wants to merge
4
commits into
Codecademy:main
Choose a base branch
from
omkarhole:Add-Dart-Map.map()-term-entry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e0eb503
Add Dart Map .map() term documentation
omkarhole e2578ec
Update title and description for map.md
omkarhole c6cc545
Revise .map() method documentation
mamtawardhani 612b152
Merge branch 'main' into Add-Dart-Map.map()-term-entry
mamtawardhani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| Title: '.map()' | ||
| Description: 'Creates a new map by transforming each key–value pair of an existing map using a provided function.' | ||
| Subjects: | ||
| - 'Computer Science' | ||
| - 'Web Development' | ||
| Tags: | ||
| - 'Dart' | ||
| - 'Data Structures' | ||
| - 'Map' | ||
| CatalogContent: | ||
| - 'learn-dart' | ||
| - 'paths/computer-science' | ||
| --- | ||
|
|
||
| In Dart, the **`.map()`** method creates 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)) | ||
| ``` | ||
|
|
||
| Here, `myMap` is the map whose entries are to be transformed. | ||
|
|
||
| **Parameters:** | ||
|
|
||
| - A function that takes two arguments: | ||
| - `key`: The key from the original map. | ||
| - `value`: The value associated with that key. | ||
| - The function must return a `MapEntry`, which defines the key–value pair in the new map. | ||
|
|
||
| **Return value:** | ||
|
|
||
| The `.map()` method returns a new `Map` containing the transformed key–value pairs. | ||
|
|
||
| > **Note:** The original map remains unchanged. | ||
|
|
||
| ## 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} | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.