TEST&복습

오늘 배운 내용 정리!

hyejeong3283 2023. 3. 2. 19:50
If you even dream of beating me you'd better wake up and apologize.
Muhammad Ali
728x90
반응형

1. forEach문

배열 변수 안에 있는 데이터의 수만큼 함수 안에 로직을 반복 실행합니다.

let arr = [10,20,30,40,50,60,70,80,90];

//for문을 이용해 출력
for(let i=0; i<arr.length; i++){
	document.write(arr[i]);
}

//forEach문
arr.forEach(function(element){ //콜백함수
	document.write(element);
});

결과값 : 10 20 30 40 50 60 70 80 90

 

2. for of

배열 안에 있는 데이터의 수만큼 반복 실행하는 방식 중 하나입니다.

const arr = [100,200,300,400,500];

    for(let i of arr){
        document.write(i);
    }

결과값 : 100 200 300 400 500

 

3. for in

배열 안에 있는 데이터의 수만큼 반복 실행하는 방식 중 하나입니다.

const arr = [100,200,300,400,500];

    for(let i in arr){
        document.write(arr[i]);
    }

결과값 : 100 200 300 400 500

 

4. map()

 

const num = [100,200,300,400,500];

    num.forEach(function(el, i, a){ //데이터값
        console.log(el);
        console.log(i);
        console.log(a)
    });

    num.map(function(el, i, a){ //배열
        console.log(el);
        console.log(i);
        console.log(a)
    });

결과값 : 100 200 300 400 500