[Javascript] 빈배열인지 확인하는법 && TypeError: Cannot read property 'length' of undefined 에러 해결방법

길현민·2022년 8월 29일
0

Javascript

목록 보기
7/14

React로 작업도중 TypeError: Cannot read property 'length' of undefined 에러가 발생했다!
데이터 송신되기전에 빈배열이 랜더링이 발생해버리는 문제였다!

{Array.isArray(buckets) && buckets.length === 0 ? ('') : (<StatusEcharts buckets={buckets} />)}

방식으로 문제를 해결했다

Array.isArray(buckets)

먼저 배열이 맞는가 아닌가를 확인한후에 논리연산자인 &&가 발생한뒤 배열안의 길이를 확인하는 방식을 사용했다!

🐔참고문헌

MDN공식문서

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

tistory

https://daramji-joa.tistory.com/25

profile
맛집탐방러

0개의 댓글