๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น(๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น)์€ ๊ตฌ์กฐํ™”๋œ ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ดํ„ฐ๋Ÿฌ๋ธ” ๋˜๋Š” ๊ฐ์ฒด๋ฅผ desrtucting(๋น„๊ตฌ์กฐํ™”, ๊ตฌ์กฐ ํŒŒ๊ดด)ํ•˜์—ฌ 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ๊ฐœ๋ณ„์ ์œผ๋กœ ํ• ๋‹นํ•˜๋Š”๊ฒƒ์„ ๋งํ•œ๋‹ค.

๐ŸŒ 36.1 ๋ฐฐ์—ด ํ‹ฐ์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น [ ]

๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•˜์—ฌ 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค. ์ด๋•Œ ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ(ํ• ๋‹น๋ฌธ์˜ ์šฐ๋ณ€)์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์ด์–ด์•ผ ํ•˜๋ฉฐ, ํ• ๋‹น ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋‹ค.

const arr = [1, 2, 3];

// ES6 ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น
// ๋ณ€์ˆ˜ one, two, three๋ฅผ ์„ ์–ธํ•˜๊ณ  ๋ฐฐ์—ด arr์„ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋งํ•˜์—ฌ ํ• ๋‹นํ•œ๋‹ค.
// ์ด๋•Œ ํ• ๋‹น ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋‹ค.
const [one, two, three] = arr;

console.log(one, two, three); // 1 2 3

๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์œ„ํ•ด์„œ๋Š” ํ• ๋‹น ์—ฐ์‚ฐ์ž ์™ผ์ชฝ์— ๊ฐ’์„ ํ• ๋‹น๋ฐ›์„ ๋ณ€์ˆ˜๋ฅผ ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด ํ˜•ํƒœ๋กœ ์„ ์–ธํ•œ๋‹ค.

const [x, y] = [1, 2];

๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋‹ค. ์ฆ‰, ์ˆœ์„œ๋Œ€๋กœ ํ• ๋‹น๋œ๋‹ค. ์ด๋•Œ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์š”์†Œ ๊ฐœ์ˆ˜๊ฐ€ ๋ฐ˜๋“œ์‹œ ์ผ์น˜ํ•  ํ•„์š”๋Š” ์—†์œผ๋ฉฐ, ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

const [a, b] = [1, 2];
console.log(a, b); // 1 2

const [c, d] = [1];
console.log(c, d); // 1 undefined

const [e, f] = [1, 2, 3];
console.log(e, f); // 1 2

const [g, , h] = [1, 2, 3];
console.log(g, h); // 1 3

// ๊ธฐ๋ณธ๊ฐ’
const [a, b, c = 3] = [1, 2];
console.log(a, b, c); // 1 2 3

// ๊ธฐ๋ณธ๊ฐ’๋ณด๋‹ค ํ• ๋‹น๋œ ๊ฐ’์ด ์šฐ์„ ํ•œ๋‹ค.
const [e, f = 10, g = 3] = [1, 2];
console.log(e, f, g); // 1 2 3

๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์—์„œ ํ•„์š”ํ•œ ์š”์†Œ๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉํ•˜๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” URL์„ ํŒŒ์‹ฑํ•˜์—ฌ protocol, host, path ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

// url์„ ํŒŒ์‹ฑํ•˜์—ฌ protocol, host, path ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
function parseURL(url = '') {
  // '://' ์•ž์˜ ๋ฌธ์ž์—ด(protocol)๊ณผ '/' ์ด์ „์˜ '/'์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ž์—ด(host)๊ณผ '/' ์ดํ›„์˜ ๋ฌธ์ž์—ด(path)์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
  const parsedURL = url.match(/^(\w+):\/\/([^/]+)\/(.*)$/);
  console.log(parsedURL);
  /*
  [
    'https://developer.mozilla.org/ko/docs/Web/JavaScript',
    'https',
    'developer.mozilla.org',
    'ko/docs/Web/JavaScript',
    index: 0,
    input: 'https://developer.mozilla.org/ko/docs/Web/JavaScript',
    groups: undefined
  ]
  */

  if (!parsedURL) return {};

  // ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์‚ฌ์šฉํ•˜์—ฌ ์ดํ„ฐ๋Ÿฌ๋ธ”์—์„œ ํ•„์š”ํ•œ ์š”์†Œ๋งŒ ์ถ”์ถœํ•œ๋‹ค.
  const [, protocol, host, path] = parsedURL;
  return { protocol, host, path };
}

const parsedURL = parseURL('https://developer.mozilla.org/ko/docs/Web/JavaScript');
console.log(parsedURL);
/*
{
  protocol: 'https',
  host: 'developer.mozilla.org',
  path: 'ko/docs/Web/JavaScript'
}
*/

๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— Rest ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์œ ์‚ฌํ•˜๊ฒŒ Rest ์š”์†Œ โ€ฆ ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Rest ์š”์†Œ๋Š” Rest ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ˜๋“œ์‹œ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•ด์•ผ ํ•œ๋‹ค.

// Rest ์š”์†Œ
const [x, ...y] = [1, 2, 3];
console.log(x, y); // 1 [ 2, 3 ]

๐ŸŒ 36.2 ๊ฐ์ฒด ํ‹ฐ์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น { }

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๊ฐ์ฒด์˜ ๊ฐ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•˜์—ฌ 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค. ์ด๋•Œ ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ(ํ• ๋‹น๋ฌธ์˜ ์šฐ๋ณ€)์€ ๊ฐ์ฒด์ด์–ด์•ผ ํ•˜๋ฉฐ, ํ• ๋‹น ๊ธฐ์ค€์€ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ํ‚ค๋‹ค. ์ฆ‰, ์ˆœ์„œ๋Š” ์˜๋ฏธ๊ฐ€ ์—†์œผ๋ฉฐ ์„ ์–ธ๋œ ๋ณ€์ˆ˜ ์ด๋ฆ„๊ณผ ํ”„๋กœํผํ‹ฐ ํ‚ค๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ํ• ๋‹น๋œ๋‹ค.

const user = { firstName: 'Ungmo', lastName: 'Lee' };

// ES6 ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น
// ๋ณ€์ˆ˜ lastName, firstName์„ ์„ ์–ธํ•˜๊ณ  user ๊ฐ์ฒด๋ฅผ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋งํ•˜์—ฌ ํ• ๋‹นํ•œ๋‹ค.
// ์ด๋•Œ ํ”„๋กœํผํ‹ฐ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์ด ์ด๋ฃจ์–ด์ง„๋‹ค. ์ˆœ์„œ๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.
const { lastName, firstName } = user;

console.log(firstName, lastName); // Ungmo Lee

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์œ„ํ•ด์„œ๋Š” ํ• ๋‹น ์—ฐ์‚ฐ์ž ์™ผ์ชฝ์— ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ํ• ๋‹น๋ฐ›์„ ๋ณ€์ˆ˜๋ฅผ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ˜•ํƒœ๋กœ ์„ ์–ธํ•œ๋‹ค.

const { lastName, firstName } = { firstName: 'Ungmo', lastName: 'Lee' };

๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐํ‚ค์™€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์ด๋ฆ„์œผ๋กœ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ํ• ๋‹น๋ฐ›์œผ๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋ฉฐ, ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

const user = { firstName: 'Ungmo', lastName: 'Lee' };

// ํ”„๋กœํผํ‹ฐ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์ด ์ด๋ฃจ์–ด์ง„๋‹ค.
// ํ”„๋กœํผํ‹ฐ ํ‚ค๊ฐ€ lastName์ธ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ln์— ํ• ๋‹นํ•˜๊ณ ,
// ํ”„๋กœํผํ‹ฐ ํ‚ค๊ฐ€ firstName์ธ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ fn์— ํ• ๋‹นํ•œ๋‹ค.
const { lastName: ln, firstName: fn } = user;

console.log(fn, ln); // Ungmo Lee

const { firstName = 'Ungmo', lastName } = { lastName: 'Lee' };
console.log(firstName, lastName); // Ungmo Lee

const { firstName: fn = 'Ungmo', lastName: ln } = { lastName: 'Lee' };
console.log(fn, ln); // Ungmo Lee

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๊ฐ์ฒด์—์„œ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ํ•„์š”ํ•œ ํ”„๋กœํผํ‹ฐ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉํ•˜๋‹ค.

const str = 'Hello';
// String ๋ž˜ํผ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ length ํ”„๋กœํผํ‹ฐ๋งŒ ์ถ”์ถœํ•œ๋‹ค.
const { length } = str;
console.log(length); // 5

const todo = { id: 1, content: 'HTML', completed: true };
// todo ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ id ํ”„๋กœํผํ‹ฐ๋งŒ ์ถ”์ถœํ•œ๋‹ค.
const { id } = todo;
console.log(id); // 1

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

function printTodo({ content, completed }) {
  console.log(`ํ• ์ผ ${content}์€ ${completed ? '์™„๋ฃŒ' : '๋น„์™„๋ฃŒ'} ์ƒํƒœ์ž…๋‹ˆ๋‹ค.`);
}

printTodo({ id: 1, content: 'HTML', completed: true });
// ํ• ์ผ HTML์€ ์™„๋ฃŒ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.

๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น๊ณผ ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ํ˜ผ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

const todos = [
  { id: 1, content: 'HTML', completed: true },
  { id: 2, content: 'CSS', completed: false },
  { id: 3, content: 'JS', completed: false }
];

// todos ๋ฐฐ์—ด์˜ ๋‘ ๋ฒˆ์งธ ์š”์†Œ์ธ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ id ํ”„๋กœํผํ‹ฐ๋งŒ ์ถ”์ถœํ•œ๋‹ค.
const [, { id }] = todos;
console.log(id); // 2

์ค‘์ฒฉ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

const user = {
  name: 'Lee',
  address: {
    zipCode: '03068',
    city: 'Seoul'
  }
};

// address ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ๊ฐ์ฒด๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์ด ๊ฐ์ฒด์˜ city ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ๊ฐ’์„ ์ถ”์ถœํ•œ๋‹ค.
const { address: { city } } = user;
console.log(city); // 'Seoul'

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— Rest ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์œ ์‚ฌํ•˜๊ฒŒ Rest ํ”„๋กœํผํ‹ฐ โ€ฆ ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Rest ํ”„๋กœํผํ‹ฐ๋Š” Rest ํŒŒ๋ผ๋ฏธํ„ฐ์™€ Rest ์š”์†Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ˜๋“œ์‹œ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•ด์•ผ ํ•œ๋‹ค.

// Rest ํ”„๋กœํผํ‹ฐ
const { x, ...rest } = { x: 1, y: 2, z: 3 };
console.log(x, rest); // 1 { y: 2, z: 3 }
profile
์ด์‚ฌ์ค‘์ž…๋‹ˆ๋‹ค!๐ŸŒŸhttps://velog.io/@devkyoung2

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด