Language/JavaScript

숫자가 정수인지 실수인지 체크를 할 경우가 있습니다. 이때 값을 나머지 연산자 %를 사용하면 구할 수 있습니다. function isInteger(number) { return number % 1 === 0;}console.log(isInteger(1)); // 결과 : trueconsole.log(isInteger(10)); // 결과 : trueconsole.log(isInteger(123456)); // 결과 : trueconsole.log(isInteger(1.1)); // 결과 : falseconsole.log(isInteger(123.12)); // 결과 : falseconsole.log(isInteger(12345.12345)); // 결과 : false 그런데 나머지 연산자 %를 이용하..
JavaScript 에서 HashTable은 대표적으로 객체(Object), Map, Set이 있습니다. 그런데 코드를 짜다보면 Key-Value 형태로 이루어진 자료구조는 객체(Object)가 대표적이었지만, ES6에 들어서서는 Map과 Set이 추가되었습니다. Map 객체 Map 객체는 Key-Value 쌍과 Key의 원래 삽입 순서를 기억합니다. 모든 값은 키 또는 값으로 사용될 수 있습니다. JavaScript에서의 Map은 java의 HashMap, python의 Dictionary와 같은 자료구조입니다. Map 생성 let map = new Map(); 위와 같이 생성자를 통해서 Map객체를 생성할 수 있습니다. Map 크기 const map1 = new Map(); map1.set('a', ..
코딩을 하다 보면 문자의 아스키코드(ASCII) or Unicode가 필요할 때가 있습니다. 이때 문자의 아스키코드를 구할 수 있는 charCodeAt()와 fromCharCode()메서드를 알아보고자 합니다. charCodeAt() string.charCodeAt(index) charCodeAt() 메서드는 index에 해당하는 문자의 unicode 값을 리턴합니다. index 인자는 필수이며, 0보다 큰 정수여야 합니다. var str = 'JavaScript' console.log(str.charCodeAt(0)); // 결과 74 console.log(str.charCodeAt(5)); // 결과 99 var stringName = '티스토리'; console.log(stringName.charC..
Array.prototype.map() & map(Number) var a = "1, 2, 3, 4, 5"; console.log(typeof(strToNum)); // 'string' var b = a.split(',').map(function(item) { return Number(item); //return parseInt(item,10); -> 10진수로 변환 }); console.log(b); //[1, 2, 3, 4, 5] for(x of bb){ console.log(typeof(x)); } //결과 : Number 문자열을 숫자형으로 변환하기 위해서는 위처럼 간단한 작업이지만 코드가 길어지게 됩니다. 이때 유용하게 사용할 수 있는 코드가 있습니다. ✅ map(Number) 입니다. var ..
지누박
'Language/JavaScript' 카테고리의 글 목록