1. Introduction to Iterators

: 반복자 소개

식료품 목록이 있고, 목록의 각 항목이 무엇인지 알고 싶다면, 각 행을 스캔하여 항목을 확인해야한다.

이러한 일반적인 반복의 과정을 배열을 반복할 때의 과정과 매우 유사하다.

그렇기에, 사용할 수 있는 도구가 바로, for 문 이다.

또한, 루프를 만들 수 있는 내장 배열 메서드에 접근함으로써, 사용할 수 있다.

반복을 도와주는 Javascript의 내장 배열 메서드들을 "반복 메서드"라고 한다.

**반복 메서드(반복자)**는, 요소를 조작하고 값을 반환하기 위해서 배열에서 호출되는 메서드를 말한다.

const artists = ['Picasso', 'Kahlo', 'Matisse', 'Utamaro'];

artists.forEach(artist => {
  console.log(artist + ' is one of my favorite artists.');
});

(결과값)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2c988afc-1a76-46ee-b340-848ada0ff533/Untitled.png

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

const squareNumbers = numbers.map(number => {
  return number * number;
});

console.log(squareNumbers);

(결과값)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/56512fdb-ab0e-4429-9e87-bfbbf164c63e/Untitled.png

const things = ['desk', 'chair', 5, 'backpack', 3.14, 100];

const onlyNumbers = things.filter(thing => {
  return typeof thing === 'number';
});

console.log(onlyNumbers);