Rest + Spread + Destructure

rest와 spread, destructure를 다양하게 활용할 수 있다.

const user = {
  name: "Tom",
  age: 28,
  password: "abc123",
};

const killPassword = ({ password, ...rest }) => rest;

const cleanedUser = killPassword(user);

console.log(cleanedUser);

{name: ‘Tom’, age: 28}


const user = {
  name: "Tom",
  age: 28,
  password: "abc123",
};

const setCountry = ({ country = "KR", ...rest }) => ({ country, ...rest });

console.log(setCountry(user));

{country: ‘KR’, name: ‘Tom’, age: 28, password: ‘abc123’}


const user = {
  NAME: "Tom",
  age: 28,
  password: "abc123",
};

const rename = ({ NAME: name, ...rest }) => ({ name, ...rest });

console.log(rename(user));

{name: ‘Tom’, age: 28, password: ‘abc123’}