728x90

forEach()

Array 요소를 제공된 함수로 한 번 실행.

값을 반환하지 않는다.(return값이 없다.)

기존 배열은 변경 가능.

// 함수를 선언해서 사용
array.forEach(function(item){
	console.log(item);
});

// es6 화살표 함수
array.forEach((item) => {
	console.log(item);
});

 

See the Pen Untitled by Kim Jae Ik (@doitdoik) on CodePen.

 

map()

모든 Array 요소가 제공된 함수로 호출될 때 새로운 array를 생성

기존의 배열을 가공하여 새로운 배열을 생성할 때 사용

기존 배열 값은 바뀌지 않고 유지

// 함수를 선언해서 사용
array.map(function(item){
	return item;
});

// es6 화살표 함수
array.map((item) => {
	return item;
});

 

See the Pen Untitled by Kim Jae Ik (@doitdoik) on CodePen.

 

728x90

+ Recent posts