text/JavaScript

.has is not a function

hoonzii 2023. 2. 27. 10:20
반응형

오늘의 error log

받아온 json은 js 내 에서 .has() 로 항목 조회 시 에러 발생

  • .has is not a function…

왜냐면... has는 set 객체 함수이니까!! 없지!!

 

나는 .hasOwnProperty(항목명) 함수로 해결~

 

Check if a key exists inside a JSON object

amt: "10.00" email: "sam@gmail.com" merchant_id: "sam" mobileNo: "9874563210" orderID: "123456" passkey: "1234" The above is the JSON ob...

stackoverflow.com

좀 더 찾아보니

dictionary (혹은 map) 타입의 key 값 확인은 보통 in을 씀

그럼 in 과 hasOwnProperty 차이는 뭘까? 참고

  1. 가장 큰 차이는 in은 operator / hasOwnProperty는 function
  2. hasOwnProperty의 경우, 객체의 상속된 properties는 false로 반환
  3. hasOwnProperty의 경우, es6의 getter, setter 여부는 false로 반환

ref) https://masteringjs.io/tutorials/fundamentals/hasownproperty

 

반응형