Design-loving front-end engineer
Ryong
Design-loving front-end engineer
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
    • Framework
    • React
      • Concept
      • Library
      • Hook
      • Component
      • Test
    • NodeJS
    • Android
      • Concept
      • Code
      • Sunflower
      • Etc
    • Flutter
      • Concept
      • Package
    • Web
    • Web
    • CSS
    • Language
    • JavaScript
    • TypeScript
    • Kotlin
    • Dart
    • Algorithm
    • Data Structure
    • Programmers
    • Management
    • Git
    • Editor
    • VSCode
    • Knowledge
    • Voice
Design-loving front-end engineer

Ryong

JavaScript

[ JS ] Rest And Spread

2022. 7. 5. 16:25

๐Ÿ”ด  Introduction to Spread

โšช  spread๋Š” ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ๊ฐ€์„œ ์š”์†Œ๋ฅผ ์ „๊ฐœํ•œ๋‹ค.

 
const friends = [1, 2, 3, 4];
const family = ["a", "b", "c"];

console.log(friends);  // [1, 2, 3, 4] => array
console.log(...friends);  // 1 2 3 4 => elements
// spread merge
console.log([friends, family]);  // [ [1, 2, 3, 4], ["a", "b", "c"] ]
console.log([...friends, ...family]);  // [1, 2, 3, 4, "a", "b", "c"]

 

๐ŸŸ   Spread Applications

 
// spread add
const first = ["mon", "tue", "wed"];
const weekend = ["sat", "sun"];
const fullWeek = [...first, "thu", "fri", ...weekend];

console.log(fullWeek);
 
// conditional spread
const lastName = prompt("Last name");

const user = {
  userName: "ryong",
  age: 28,
  ...(lastName !== "" && { lastName })
};

console.log(user);  // {userName: "ryong", age: 28, lastName: "lalala"}

 

๐ŸŸก  Intro to Rest Parameters

 
const bestFriendMaker = (firstOne, ...rest) => {
  console.log(`My best friend is ${firstOne}`);  // ~ ryong
  console.log(rest);  // ["lin", "hyun", "hun"]
};

bestFriendMaker("ryong", "lin", "hyun", "hun");

 

๐ŸŸข  Rest + Spread + Destructure Magic

โšช  object๋ฅผ ์ง€์šฐ๊ฑฐ๋‚˜ ์ •๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ

 
const user = {
  name: "ryong",
  age: 28,
  password: 12345
};

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

const cleanUser = killPassword(user);

console.log(cleanUser);  // {name: "ryong", age: 28}

โšช  ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •ํ•˜๊ธฐ

 
const user = {
  name: "ryong",
  age: 28,
  password: 12345
};

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

console.log(setCountry(user));
// {country: "KR", name: "ryong", age: 28, password: 12345}

โšช  key ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ

 
const user = {
  NAME: "ryong",
  age: 28,
  password: 12345
};

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

console.log(rename(user));
// {name: "ryong", age: 28, password: 12345}

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ JS ] Promises  (0) 2022.07.06
[ JS ] For Loop  (0) 2022.07.05
[ JS ] Destructuring  (0) 2022.07.05
[ JS ] Array  (0) 2022.07.05
[ JS ] Strings  (0) 2022.07.05
    'JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ JS ] Promises
    • [ JS ] For Loop
    • [ JS ] Destructuring
    • [ JS ] Array
    Design-loving front-end engineer
    Design-loving front-end engineer
    ๋””์ž์ธ์— ๊ด€์‹ฌ์ด ๋งŽ์€ ๋ชจ๋ฐ”์ผ ์•ฑ ์—”์ง€๋‹ˆ์–ด Ryong์ž…๋‹ˆ๋‹ค.

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”