비 구조화 할당
let arr=["one","two","three"]
let one = arr[0];
let two = arr[1];
let three =arr[2];
console.log = arr[2];
==>
let arr=["one","two","three"]
let [ one, two, three] =arr;
console.log(one, two, three);
비구조화 할당은 배열의 순서에 따라 할당할 수 있게 하는 것이다.
SWAP
let a =10;
let b=20;
[a,b]=[b,a]
console.log(a,b)
객체에서의 비구조화 할당
let object ={one: 'one',two:'two',three:'three'};
let one=object.one;
let two=object.two;
let three=object.three;
console.log(one,two,three);
==> 이러한 방법은 객체의 이름을 계속반복해야 하는 단점이 존재한다.
let object ={one: 'one',two:'two',three:'three'};
let {one, two, three}=object;
console.log(one,two,three);
순서와는 상관없이 키값을 이용해서 적용한다.
키값으로 구조화를 할당하지만 다른 이름을 사용하고 싶다면
let {name: myname, one, two }이런식으로 사용하면된다.
spread 연산자
const cookie= {
base:"cookie",
madeIn:"korea"
};
const chocohipCookie={
...cookie,
toping: "chocochup"
}
const blueberryCookie={
...cookie,
toping: "chocochup"
}
const strawberryCookie={
...cookie,
toping: "chocochup"
}
...: spread 연산자
중복된 내용을 생략해준다.
활용
const noTopingCookies =["촉촉한쿠키","안촉촉한쿠키"];
const topingCookies = ["바나나쿠키","블루베리쿠키","딸기쿠키","초코칩쿠키"];
const allCookies =[...noTopingCookies,...topingCookies];
console.log(allCookies);
'개발 > javascript' 카테고리의 다른 글
[javascript]Promise & 콜백지옥 (0) | 2023.07.25 |
---|---|
[javascript]동기&비동기 (0) | 2023.07.24 |
[javascript] 응용1 (0) | 2023.07.04 |
[javascript]기본정리5 (0) | 2023.07.03 |
[javascript]기본정리4 (0) | 2023.07.02 |