[JS] JavaScript에서 type이 Array인지 확인하는 법

Leo Bang·2021년 10월 20일
0
post-thumbnail

Problem

리액트 네이티브에서 axios로 받아온 json 데이터의 타입을 확인하고 싶어서 typeof 메소드를 이용했다.

분명 prototype은 Array라고 나오는데, typeof로 확인하면 object가 나온다...
이거 때문에 1시간 동안 씨름했다. 별거 아니었는데,,,

solution

array 역시 object type 중 하나이기 때문에 typeof 메소드를 이용해서 확인하면 object라고 나온다고 한다.

보다 명시적으로 해당 객체가 array임을 확인하고 싶다면
Array.isArray() 메소드를 이용해야 한다.

const thisIsList = [1, 2, 3, 4, 5];

console.log(typeof thisIsList);
// object
console.log(Array.isArray(thisIsList));
// true     
profile
me, myself and code

0개의 댓글