728x90
forEach()
forEach() 문은 배열을 순회하는 함수입니다.
array.forEach(func(value, index, array))
기본 구조는 이렇게 되어있습니다.
value는 현재 요소, index는 현재 index번호, array는 배열입니다.
실습하기
1. value
let c = console.log
let list = ['Mount' , 'Silva' , 'Gallagher' , 'Kepa' , 'Harvertz']
list.forEach((value) => {
c(value)
})
value 만을 인자로 넣었을 때, 리스트에 있는 값만 출력이 됩니다.
2. index
let c = console.log
let list = ['Mount' , 'Silva' , 'Gallagher' , 'Kepa' , 'Harvertz']
list.forEach((value,index) => {
c(`index : ${index} , item : ${value}`)
})
index 까지 인자를 넣었을 때 index 번호까지 출력시킬 수 있습니다.
3. array
let c = console.log
let list = ['Mount' , 'Silva' , 'Gallagher' , 'Kepa' , 'Harvertz']
list.forEach((value,index,array) => {
c(`index : ${index} , value : ${value} , array[${index}] : ${array[index]}`)
})
array 까지 인자를 넣었을 때 array로 접근해 array에 요소들을 출력시킬 수 있습니다.
728x90
'Javascript' 카테고리의 다른 글
filter() (0) | 2023.02.02 |
---|---|
map() (0) | 2023.01.30 |
For 반복문 (0) | 2023.01.18 |
Sort() (0) | 2023.01.15 |
Local Stroage 이용하기 (1) | 2022.09.19 |