개발 창고/Web
[Javascript] Json 데이터에 해당 파라미터 값이 있는지 여부 확인하기
로이제로
2020. 10. 20. 16:43
반응형
Json 데이터에 호출하려는 키값이 있는지 확인하는 방법에는 아래의 5가지가 있습니다 (제가 아는 기준)
let test = {type:"유형", name:"이름"}
console.log("1. 프로퍼티체크 (json.hasOwnProperty[키값])");
console.log("있는 경우(type) : " + test.hasOwnProperty("type"));
console.log("없는 경우(price) : " + test.hasOwnProperty("price"));
console.log("2. 직접호출1 (json[키값] != undefined)");
console.log("있는 경우(type) : " + (test["type"] != undefined));
console.log("없는 경우(price) : " + (test["price"] != undefined));
console.log("3. 직접호출2 (json.키값 != undefined)");
console.log("있는 경우(type) : " + (test.type != undefined));
console.log("없는 경우(price) : " + (test.price != undefined));
console.log("4. 포함여부 체크 (키값 in json)");
console.log("있는 경우(type) : " + ("type" in test));
console.log("없는 경우(price) : " + ("price" in test));
console.log("5. 포함여부 체크 (키값 in json)");
console.log("있는 경우(type) : " + ("type" in test));
console.log("없는 경우(price) : " + ("price" in test));
입맛에 맞게 골라쓰시면 될겁니다.
반응형