JavaScript map( ) Function

Theo·2024년 5월 1일

JavaScript_Function

목록 보기
3/3
post-thumbnail

이론으로 보는 map( )

  • 원본배열의 요소들을 다른 형태로 변환하여 새로운 배열에 담을 수 있다.

코드로 보는 map( )

  • fruits를 더이상 과일을 나타내는 객채가 아니라 가격을 나타내는 객채로 봐꾸고 싶다면
const fruits = [
	{ naem: '사과', price: 300 },
    { naem: '바나나', price: 700 },
    { naem: '오렌지', price: 500 },
    { naem: '레몬', price: 1000 },
];

const priceTags = fruits.map((fruit) => {
	// 요소를 어떤식으로 변환할건지
    retrun '${fruit.name} : ${fruit.price}원';
});

console.log(fruits);	// 	0: { naem: '사과', price: 300 }
    					//	1: { naem: '바나나', price: 700 }
    					//	2: { naem: '오렌지', price: 500 }
    					//	3: { naem: '레몬', price: 1000 }
                        //	length: 4
console.log(priceTags);	// ['사과 : 300', 바나나 : 700, '오렌지 : 500, '레몬 : 1000']
profile
Logbook

0개의 댓글