-
🚨 array.filter의 값이 undefined로 나올 때!JavaScript 2021. 4. 22. 17:42
const selectedCode = this.list.find(e => e.code === this.data);데이터 옵션에 정의된 this.data와 e.code이 같은 list의 요소를 찾으려고 할 때,
이렇게 filter함수를 사용했다.
그런데 selectedCode가 계속 undefined로 떴다.
MDN에서 테스트를 해보았을 때도 문제 없이 잘 나왔다.

검색해본 결과, 화살표 함수에서는 this가 참조하는 대상이 다르기 때문이라고 했다.
const temp = this.data; const selectedCode = this.list.find(e => e.code === temp);✔ 대상 데이터를 새로운 변수에 담아 그 참조값으로 비교하였다!
정처기 실기 시험이 끝나면 this에 대한 공부를 더해야겠다.
참고 | stackoverflow.com/questions/50732786/returns-undefined-when-finding-an-item-in-an-array-in-vue
'JavaScript' 카테고리의 다른 글
Javascript ES6 스프레드 연산자 (0) 2021.07.25 크롬 앱, momentum 클론 코딩 (0) 2021.01.01