-
javaScript) map 자료형 for문 사용하기Programing Language/JavaScript 2021. 5. 20. 10:46728x90반응형This file contains 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
//참고 https://javascript.info/map-set new Map() – creates the map. map.set(key, value) – stores the value by the key. map.get(key) – returns the value by the key, undefined if key doesn’t exist in map. map.has(key) – returns true if the key exists, false otherwise. map.delete(key) – removes the value by the key. map.clear() – removes everything from the map. map.size – returns the current element count. let map = new Map(); map.set('1', 'str1'); // a string key map.set(1, 'num1'); // a numeric key map.set(true, 'bool1'); // a boolean key // remember the regular Object? it would convert keys to string // Map keeps the type, so these two are different: alert( map.get(1) ); // 'num1' alert( map.get('1') ); // 'str1' alert( map.size ); // 3 //////////////////////////////////////////////////////////// ///// for문 //////////////////////////////////////////////// let recipeMap = new Map([ ['cucumber', 500], ['tomatoes', 350], ['onion', 50] ]); // iterate over keys (vegetables) for (let vegetable of recipeMap.keys()) { alert(vegetable); // cucumber, tomatoes, onion } // iterate over values (amounts) for (let amount of recipeMap.values()) { alert(amount); // 500, 350, 50 } // iterate over [key, value] entries for (let entry of recipeMap) { // the same as of recipeMap.entries() alert(entry); // cucumber,500 (and so on) } //////////////////////////////////////////////////////////// //////////// forEach 문///////////////////////////////////// // runs the function for each (key, value) pair recipeMap.forEach( (value, key, map) => { alert(`${key}: ${value}`); // cucumber: 500 etc }); 728x90반응형'Programing Language > JavaScript' 카테고리의 다른 글
javaScript) 정규 표현식 모음 정리 (0) 2021.05.28 javaScript) input 태그 포커스 인/ 아웃 이벤트 캐치하기 (0) 2021.05.27 javaScript) 랜덤(난수) 생성하기 (0) 2021.05.20 CSS+HTML)부모 div영역의 높이에서 이미지와 텍스트 수직 정렬하기 (0) 2021.04.10 CSS) Div안에 텍스트 가운대 위치시키기 (0) 2021.04.10