반응형
map함수
-
map()프레임워크/리액트 React 2023. 1. 17. 01:33
리액트에서 반복문 처리 JSX에서 자바스크립트 사용시 중괄호를 쓰고, 안에다 입력한다.주로 사용했던 조건문인 if문 대신 {} 안에서 삼항 연산자를 사용했듯,반복문을 처리할 때는 for문 대신 map() 을 이용해 반복문을 처리해야 한다. array.map() ⭐반복문 사용 시 유용하다! array의 자료 개수만큼 함수 안의 코드를 실행해준다. 함수의 파라미터는 array 안에 있던 자료이다. return 오른쪽에 있는 것 array에 담아준다. //array.map(콜백함수); array.map(function() { 실행할 코드 }); [1,2,3].map(function(a) { console.log(a) // 1,2,3 순서대로 나옴 }); [1,2,3].map(function(a) { retur..