[JS] JSON 객체에서 원하는 key 값 찾기

호두파파·2021년 1월 24일
0

메모

목록 보기
5/18

JavaScript가 제공하는 hasOwnProperty 메서드를 통해서 json 객체 안에서 원하는 키가 존재하는지 살펴볼 수 있다.

{

code: '1111',

message: 'success'

}

let checkCode = res.hasOwnProperty('code'); // true
let checkMsg = res.hasOwnProperty('message'); // true
let checkData = res.hasOwnProperty('data'); // false

위처럼 json 객체 안에 존재하는 key의 존재유무를 boolean형으로 체크할 수 있다.

profile
안녕하세요 주니어 프론트엔드 개발자 양윤성입니다.

0개의 댓글