Skip to main content

Command Palette

Search for a command to run...

08_JavaScript : spread & rest

Object, Array, ...rest

Updated
•2 min read•View as Markdown
08_JavaScript : spread & rest

(Object) spread

const cake = {
    name: 'cakeeee';
};

const cuteCake = {
    name: 'cakeeee',
    attribute: 'cute'
};

const purpleCuteCake = {
    name: 'cakeeee',
    attribute: 'cute',
    color: 'purple'
};

console.log(cake);
console.log(cuteCake);
console.log(purpleCuteCake);

-> Use 'spread' to make it shorter

const cake = {
    name: 'cakeeee'
};
const cuteCake = {
    ...cake,
    attribute: 'cute'
};
const purpleCuteCake = {
    ...cuteCake,
    color: 'purple'
};

console.log(cake);
console.log(cuteCake);
console.log(purpleCuteCake);

(Array) spread

const animals = ['dog', 'cat', 'bird']
const anotherAnimals = [...animals, 'panda'];
const manyAnimals = [...animals, 'panda', ...animals];
console.log(animals);
console.log(anotherAnimals);
console.log(manyAnimals); // ['dog', 'cat', 'bird', 'panda', 'dog', 'cat', 'bird']

Rest

  • object, array, parameters of the function
// object 
const purpleCuteCake = {
    name: 'cakeeee',
    attribute: 'cute',
    color: 'purple'
};
// 비구조화 할당 문법과 함께 사용(특히 object와 array)
const { color, ...rest } = purpleCuteCake; //rest 이름 바꿔도 됨
console.log(color); //purple
console.log(rest); //{ name: 'cakeeee', attribute: 'cute' }

const { attribute, ...cake01 } = rest;
console.log(attribute); // { name: 'cakeeee', attribute: 'cute'}
console.log(cake01); // { name: 'cakeeee' }
// Array
const numbers = [0, 1, 2, 3, 4, 5, 6];
const [one, ...rest] = numbers; // ...rest는 맨 앞에 못 씀(SyntaxError)
console.log(one); //0
console.log(rest); //[1, 2, 3, 4, 5, 6]
// parameters of the function
function sum(a, b, c, d, e, f, g) {
    let sum = 0;
    if (a) sum += a;
    if (b) sum += b;
    if (c) sum += c;
    if (d) sum += d;
    if (e) sum += e;
    if (f) sum += f;
    if (g) sum += g;
    return sum;
}
const result = sum(1,2,3,4,5,6); // sum function has 7 parameters but I put only 6
// g is undefined so the result is NaN
console.log(result);

-> 함수의 파라미터가 몇개 될지 모르는 상황에서 rest 파라미터 유용

function sum(...rest) {
    return rest;
}
const result = sum(1,2,3,4,5,6);
console.log(result); // [1,2,3,4,5,6]
function sum(...rest) {
    return rest.reduce((acc, current) => acc + current, 0);
}
const result = sum(1,2,3,4,5,6);
console.log(result); // 21

-> 만약 배열 안의 원소들을 모두 파라미터로 넣어주고 싶다면

function sum(...rest) {
    return rest.reduce((acc, current) => acc + current, 0);
}
const numbers = [1,2,3,4,5,6];
const result = sum( //uncomfortable
    numbers[0],
    numbers[1],
    numbers[2],
    numbers[3],
    numbers[4],
    numbers[5]
);
console.log(result);

-> Make it shorter

function sum(...rest) {
    result rest.reduce((acc, current) => acc + current, 0);
}
const numbers = [1,2,3,4,5,6];
const result = sum(...numbers); //배열의 원소 하나하나를 파라미터로 넣어주기 
console.log(result);

More from this blog

Next.js의 Routing

라우팅은 네트워크 용어로서 네트워크상의 주소로 이동하여 해당 주소에 연결되어있는 데이터를 사용하는 일련의 과정을 의미 라우터는 라우팅을 수행할 수 있는 장치 pages (혹은 src/pages) 폴더 아래에 파일을 추가하는 방식으로 각 컴포넌트에 URL을 부여 install - 리액트에서는 라우터를 제공하지 않기 때문에 react-router-dom이라는 패키지를 설치 router 객체 확인 가능 동적 라우팅(Dynamic Rout...

Aug 30, 20232 min read
Next.js의 Routing

What I've learned on my tech journey

26 posts

Frontend Developer👩‍💻