-
[자바스크립트] map 메서드와 Map 객체의 차이FrontEnd/JavaScript 2023. 6. 24. 14:36
1. map 메서드
- js에서 map 함수는 배열에서 사용되는 메서드
- 배열의 각 요소에 대해 주어진 함수를 호출하여 새로운 배열을 생성함.
- 배열의 각 용소를 변형 / 추출하여 새로운 배열을 만들 때 사용
const nums = [1,2,3,4,5] const doubleNums = nums.map((n)=>n*2) //[2,4,6,8,10]
2. Map 객체
- js의 내장 객체로 키- 값 쌍을 저장하는 자료구조
- 일반적인 객체와 다르게 키 - 값의 연결을 유지하며 순서가 보장 됨
- 기본적으로 삽입(set), 조회(get), 삭제(clear,delete) 가 있음
- 문자열 아닌 값도 키로 사용 가능
const mp = new Map() mp.set(1,3) mp.set("asd","asdasd") console.log(mp.get(1)) //3 console.log(mp.get("asd")) //"asdasd"
'FrontEnd > JavaScript' 카테고리의 다른 글
[JS] url 다루기 (0) 2023.08.19 [자바스크립트] 이터러블 개념과 FE에서의 사용처 (0) 2023.06.24 [JavaScript] this에 대해 (feat. bind) (0) 2022.12.11 Axios에 대하여 (vs Fetch) (0) 2022.12.07 JSON Fetch 사용하기 (0) 2022.12.04