JavaScript

Javascript 에서 점점점 (…) Three dots이란?

dev.mk 2022. 9. 21. 15:02
반응형

영어로는 three dots 로 일컫는다.

 

용도

- 보통  배열과 배열을 합칠 때 사용.

- 오브젝트 끼리도 합칠 있다.

 

1. 배열과 배열을 합치기

let a = ['A', 'AA'];
console.log(a);

//결과
//[ 'A', 'AA' ]
그냥 배열을 출력 했을때는 우리가 예상한대로 나온다.
let a = ['A', 'AA'];
console.log(…a);

//결과
//A AA
…a를 출력 하면 []배열 안에 값이 있지 않고 []가 벗겨진채로 출력이 된다.
let a = ['A', 'AA'];
let b = ['B', 'BB']

console.log([…a, …b]);

// 결과
// [ 'A', 'AA', 'B', 'BB' ]
a,b배열이 합쳐진 상태로 출력이 된다.
let a = ['A', 'AA'];
let b = ['B', 'BB']

[…a, …b].forEach(item => console.log(item));
 
// 결과
// A
// AA
// B
// BB
a,b 배열이 합쳐졌기 떄문에 합친 배열만큼 반복문이 실행된다.

 

2.오브젝트 끼리 합치기

let obj1 = {
    class: 'A',
    name: 'Hong',
    age: '20'
};

let location = 'Seoul';

let sumValues = {
    ...obj1,
    location
}

//결과
//console.log(sumValues);
{class: 'A', name: 'Hong', age: '20', location 'Seoul'}

 

3.오브젝트 + 배열 합치기

let arry1 = 
[
    {
        "maxCount" : 0,
        "resCount" : 1,
    }, {
        "maxCount" : 8,
        "resCount" : 1,
    } 
]

let obj1 = {
    "aCount" : 10,
    "bCount" : 20
}

let ojbMerge = { obj1, ...{arry2 : arry1} };

결과

반응형