FrontEnd/Javascript
[ Javascript ] 전개 연산자 ' ... '
jimin-log
2023. 8. 14. 16:32
전개 연산자 ' ... '
각각의 아이템으로 변경해준다.
const fruits = ['Apple','Banana', 'Cherry'];
console.log(fruits);
//결과 값 : ['Apple','Banana', 'Cherry']
console.log(...fruits);
//결과 값 : Apple,Banana, Cherry
1. 전개 연산자 활용방법
const fruits = ['Apple','Banana', 'Cherry'];
function toObject(a,b,c){
return :{
a: a,
b: b
b: c
}
}
console.log(toObject(...fruits))
//결과값 {a:'Apple',b:'Banana', c:'Cherry'}
//동일 한 결과
console.log(toObject(fruits[0],fruits[1],fruits[2]))
2. 매개변수에서의 전개 연산자
const fruits = ['Apple','Banana', 'Cherry','Orange'];
// ...c : rest parameter 나머지 매개 변수
function toObject(a,b,..c){
return :{
a: a,
b: b,
b: c
}
}
console.log(toObject(...fruits))
//결과값 {a:'Apple',b:'Banana', c:['Cherry','Orange']}
3. 축약형 방법
매개 변수의 이름과 변수의 이름이 같으면 아래와 같이 축약이 가능하다.
const toObject = (a,b,..c)=> ({a,b,c})