키 가져오기
Coddy Dart 여정의 기초 섹션에 포함된 레슨 — 94개 중 61번째.
맵의 keys 속성은 맵의 모든 키를 포함하는 반복 가능한 컬렉션을 반환합니다. 이것은 값 없이 키만 접근할 수 있게 합니다.
학생 점수의 맵을 생성하세요:
Map<String, int> scores = {
'Alice': 95,
'Bob': 85,
'Charlie': 90
};맵에서 모든 키를 가져오기:
Iterable<String> studentNames = scores.keys;
print('Student names: $studentNames');위의 코드를 실행한 후 출력은 다음과 같습니다:
Student names: (Alice, Bob, Charlie)for-in 루프를 사용하여 모든 키를 반복할 수도 있습니다:
for (String name in scores.keys) {
print(name);
}위 코드를 실행하면 출력은 다음과 같습니다:
Alice
Bob
Charlie챌린지
초급이 챌린지에서, 맵에서 모든 키를 가져오는 keys 속성을 사용하는 연습을 할 것입니다. keys 속성은 맵의 모든 키를 포함하는 반복 가능한 컬렉션을 반환합니다.
fruitPrices 맵에서 모든 키를 가져와 fruitNames 변수에 저장하는 코드를 완성하세요.
Expected output:
(apple, banana, orange)치트 시트
keys 속성은 맵의 모든 키를 포함하는 반복 가능한 컬렉션을 반환합니다:
Map<String, int> scores = {
'Alice': 95,
'Bob': 85,
'Charlie': 90
};
Iterable<String> studentNames = scores.keys;
print('Student names: $studentNames'); // Output: Student names: (Alice, Bob, Charlie)for-in 루프를 사용하여 키를 반복할 수 있습니다:
for (String name in scores.keys) {
print(name);
}직접 해보기
void main() {
// 과일과 그 가격의 맵
Map<String, double> fruitPrices = {
'apple': 1.99,
'banana': 0.99,
'orange': 1.49
};
// TODO: fruitPrices 맵에서 모든 키를 가져와서
// fruitNames 변수에 저장하세요
var fruitNames;
// 이것은 모든 과일 이름을 출력할 것입니다
print(fruitNames);
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.