반응형
Q. find의 인자로 들어간 변수 'article'?
private articles: { id: number; title: string; content: string }[] = [];
update(id: number, updatePostDto: UpdatePostDto) {
const {content, password} = updatePostDto;
const article = this.articles.find((article)=>article.id === id);
}
여기서 사용된 article은 JavaScript의 Array.prototype.find 메서드에서 콜백 함수의 인자로 넘어온 변수다.
find 메서드는 배열의 각 요소에 대해 콜백 함수를 실행하며, 콜백 함수의 첫 번째 인자로 현재 처리하고 있는 배열의 요소를 전달한다. 이 코드의 경우, article은 this.articles 배열의 각 요소를 차례대로 나타낸다.
this.articles.find((article) => article.id === id);에서 article은 this.articles 배열에서 순회하는 현재 요소를 가리킨다. 배열의 각 객체에는 id 속성이 있으며, id 속성이 함수에 전달된 id 변수와 동일한지 여부를 판단하여 조건에 맞는 첫 번째 요소를 반환한다.
만약 this.articles 배열 내에 id 값이 전달된 id와 일치하는 요소가 있다면, 해당 article 객체가 반환된다.
반응형
'TIL' 카테고리의 다른 글
Nest.js 프로젝트에 TypeORM 적용하기 (0) | 2024.03.08 |
---|---|
lodash 라이브러리 함수 정리 (0) | 2024.03.08 |
캐시에 대해서 (0) | 2024.03.06 |
TypeScript에 대하여 (0) | 2024.03.05 |
Redis에 대해서 (1) | 2024.02.29 |