.forEach()
기본형
빈 array를 하나 만들고
forEach함수에 double 함수를 집어 넣는다.
빈 array에 하나씩 결과를 담는다.
함수 안 함수
double 함수를 밖으로 빼지 않고, forEach 안에 익명함수로 들어가서 바로 적용 시켜버린다.
Map( )
loop through the array
map 함수 안에 다른 함수를 집어 넣어 하나씩 실행되도록 한다.
array 작업 시에는 꼭 아이템에 key를 적어줘야 한다.
빈 array 만들 필요없이 바로 map()으로 계산된 함수를 새 array에 담아 버린다.
map함수 안에 double 함수 넣기
Filter( )
기존 array에서
filter()함수에 컨디션을 지정하고
true 인 것만 새 array에 담는다.
forEach 로 나타내면
filter()로 나타내면
10보다 큰 50, 30 이 나옴
Reduce( )
array 에 있는 아이템 다 계산되고 마지막 결과가 나옴
89 = 2 + 50 + 2 + 30 + 5
Reduce()
accumulate a value by doing sth to each item in an array.
var numbers = [2, 50, 3, 30, 5];
var newNumber = numbers.reduce(function(accumulator, currentNumber){
console.log("accumulator: " + accumulator);
console.log("currentNumber: " + currentNumber);
return accumulator + currentNumber;
});
console.log(newNumber);
accumulator: 처음에는 첫 아이템 나오고 나서 더해감
find( )
find ( )
find the first item that matches from an array.
찾는 게 처음 나온 아이템에 스탑함
해당 값 나옴
findIndex()
찾는 게 처음 나왔을 때 해당 인덱스 알려줌
인덱스 나옴
'React > React Basic' 카테고리의 다른 글
[React] Arrow Function (0) | 2022.03.16 |
---|---|
[React] 이모티콘 자료에서 뜻만 빼서 100자 까지만 보이기 (0) | 2022.03.15 |
[React] Mapping Data | 이모티콘 사전 | dl dt dd (0) | 2022.03.14 |
[React] Map() | functional programming | Mapping Data to Components (0) | 2022.03.13 |
[React] DevTools | Avatar 이미지 따로 떼서 관리하기 |DevTools로 디버깅 (0) | 2022.02.24 |
댓글