반응형
displayListByName(): string[] {
// map - 기존 배열을 변환해서 새로운 배열을 만든다.
// 아래 코드는 contact 배열의 name으로만 새로운 배열을 만든 코드다
return this.contacts.map(contact => contact.name);
}
displayListByAddress(): string[] {
return this.contacts.map(contact => contact.address);
}
let heroes = [
{ name: 'Tony', age: 30 },
{ name: 'Captain', age: 100 },
];
heroes.map(function (hero) {
return hero.name;
});
예) herose 배열에서 name만 뽑아서 새로운 배열을 만들고 싶은 경우에 map을 사용한다.
반응형
'TypeScript > TypeScript - 기초부터 실전까지' 카테고리의 다른 글
22. 인터페이스와 제네릭을 이용한 타입 추론 방식 (0) | 2023.10.14 |
---|---|
21. 타입 추론 (Type Inference) (0) | 2023.10.14 |
19. 클래스 선언부 타입 정의 | 메서드 타입 정의 | enum을 이용한 타입 정의 (0) | 2023.10.14 |
18. API 함수 타입 정의 | Promise<Contact[]> (0) | 2023.10.14 |
17. tsconfig.json, .eslintrc.js 설정 (0) | 2023.10.14 |