반응형
    
    
    
  
		                                        Notice
		                                        
										
                                    
                                    
                                        Recent Posts
                                        
                                    
                                    
                                        Recent Comments
                                        
                                    
                                    
                                        Link
                                        
                                    
                                | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | 
| 12 | 13 | 14 | 15 | 16 | 17 | 18 | 
| 19 | 20 | 21 | 22 | 23 | 24 | 25 | 
| 26 | 27 | 28 | 29 | 30 | 31 | 
                                        Tags
                                        
                                    
                                    - 배열
- json
- date
- HashMap
- 이클립스
- 문자열
- map
- html
- string
- CMD
- CSS
- Array
- 정규식
- ArrayList
- 자바
- table
- Java
- javascript
- IntelliJ
- Eclipse
- 인텔리제이
- Button
- input
- Visual Studio Code
- list
- vscode
- replace
- 자바스크립트
- js
- 이탈리아
                                        Archives
                                        
                                    
                                    - Today
- Total
어제 오늘 내일
[Javascript] Object(객체) key 존재 여부 확인하기 본문
Object에 특정 이름의 key가 존재하는지 확인하기 위한 방법입니다.
- in 연산자
- hasOwnProperty()
1. in 연산자
객체에 특정 키가 존재하는지 체크하기 위해서,
in 연산자를 사용할 수 있습니다.
객체에 특정키가 존재하면 true를 리턴하고, 존재하지 않으면 false를 리턴합니다.
const obj = {
  name: 'anna',
  age: 20
};
document.write('name' in obj); // true
document.write('<br>');
document.write('address' in obj); // false
2. hasOwnProperty()
hasOwnProperty() 메소드를 이용할 수도 있습니다.
마찬가지로, 객체에 특정키가 존재하면 true를 리턴하고, 그렇지 않으면 false를 리턴합니다.
const obj = {
  name: 'anna',
  age: 20
};
document.write(obj.hasOwnProperty('name')); // true
document.write('<br>');
document.write(obj.hasOwnProperty('address')); // false
3. in 연산자와 hasOwnProperty()
const obj = {
  name: 'anna',
  age: 20
};
document.write('=== in === <br>');
document.write('__proto__' in obj); // true
document.write('<br>');
document.write('constructor' in obj); // true
document.write('<br>=== hasOwnProperty() === <br>');
document.write(obj.hasOwnProperty('__proto__')); // false
document.write('<br>');
document.write(obj.hasOwnProperty('constructor')); // false
'in' 연산자는
객체에서 상속받은 속성까지 존재여부를 체크합니다.
따라서, 위 예제를 보면
'in' 연산자를 사용한 경우, 상속받은 속성인 '__proto__', 'constructor' 등의 속성까지 존재여부를 체크합니다.
반면, hasOwnProperty()는 객체에 정의된 속성만을 체크합니다.
따라서, 일반적인 경우(객체에 특정 key가 존재하는지 체크하려는 경우)에는
'in' 연산자보다는 'hasOwnProperty()' 를 사용하는 것이 더 좋습니다.
반응형
    
    
    
  'IT > Javascript' 카테고리의 다른 글
| [Javascript] 왼쪽에 있는 0 제거하기 (replace, 숫자 변환) (0) | 2023.12.16 | 
|---|---|
| [Javascript] 원단위 절사하기 (0) | 2023.06.26 | 
| [Javascript] 자식 노드 모두 삭제하기 (0) | 2022.07.22 | 
| [Javascript] 체크박스 초기화하기 (0) | 2022.07.21 | 
| [Javascript] 특정 문자 제거하기 - 모두 제거, 대소문자 구분 (replace) (0) | 2022.07.20 | 
                              Comments