[DEV] 기록

[javascript] common-lang3의 ObjectUtils.isEmpty()와 같은 코드를 작성하고 싶다면?

꾸준함. 2023. 8. 31. 09:01

개요

useEffect React Hook을 통해 특정 객체가 비어있을 때만 업데이트하는 로직을 작성하려는데 common-lang3의 ObjectUtils.isEmpty()와 같은 동작을 하는 코드가 있으면 좋겠다 싶어 비슷하게 구현한 코드를 찾았습니다.

 

코드

object가 확인하고 싶은 객체라고 할 때 코드는 아래와 같습니다.

 

 

Object.keys() 메서드는 object 객체의 배열 또는 모든 키를 반환합니다.

그리고 반환된 값의 길이를 확인하여 빈 개체인지 여부를 확인할 수 있습니다.

 

출처

 

https://stackoverflow.com/questions/48353471/checking-if-a-state-object-is-empty

 

Checking if a state object is empty

I have a react js state object and would like to execute some code if the object is empty. Is there something wrong with my logic because the code inside the if block is not getting executed. if ...

stackoverflow.com

 

반응형