Skip to content

Latest commit

ย 

History

History
154 lines (107 loc) ยท 3.52 KB

2019-04-11-Destructuring.md

File metadata and controls

154 lines (107 loc) ยท 3.52 KB

2019๋…„ 4์›” 11์ผ

Destructuring


Destructuring์€ ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด์—์„œ ํ•„์š”ํ•œ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

1. Array destructuring

  • ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์˜ ์ถ”์ถœ / ํ• ๋‹น ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์ด๋‹ค.

ES5

// ES5 Destructuring
var arr = [1, 2, 3];

var one   = arr[0];
var two   = arr[1];
var three = arr[2];

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

ES6

// ES6 Destructuring
const arr = [1, 2, 3];

// ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์š”์†Œ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹น
const [one, two, three] = arr;

console.log(one, two, three); // 1 2 3
let a, b, c;
[a, b, c] = [1, 2, 3];

// ์œ„์˜ ๊ตฌ๋ฌธ๊ณผ ๋™์ผํ•˜๋‹ค
let [a, b, c] = [1, 2, 3];
let a, b, c;

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

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

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

[a, , c] = [1, 2, 3];
console.log(a, c); // 1 3

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

[a, b = 10, c = 3] = [1, 2];
console.log(a, b, c); // 1 2 3

// spread ์—ฐ์‚ฐ์ž
[a, ...c] = [1, 2, 3];
console.log(a, b); // 1 [ 2, 3 ]
  • ES6์˜ ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์€ ๋ฐฐ์—ด์—์„œ ํ•„์š”ํ•œ ์š”์†Œ๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค.
  • ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” Date ๊ฐ์ฒด์—์„œ ๋…„๋„, ์›”, ์ผ์„ ์ถ”์ถœํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.
const today = new Date();
console.log(today); // 2019-05-01T15:38:25.837Z
const formattedDate = today.toISOString().substring(0, 10);
console.log(formattedDate); // 2019-05-01
const [year, month, day] = formattedDate.split('-');
console.log([year, month, day]); // [ '2019', '05', '01' ]

2. Object destructuring

  • ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์˜ ์ถ”์ถœ / ํ• ๋‹น ๊ธฐ์ค€์€ ๊ฐ์ฒด์˜ ํ‚ค(key)๊ฐ’์ด๋‹ค.
// ES5 Destructuring
var obj = { firstName: 'Sony', lastName: 'Park' };

var firstName = obj.firstName;
var lastName  = obj.lastName;

console.log(firstName, lastName); // Sony Park
// ES6 Destructuring
const obj = { firstName: 'Sony', lastName: 'Park' };

const { firstName, lastName } = obj;

console.log(firstName, lastName); // Sony Park
const { key1, key2 } = { key1 : 'a', key2 : 'b' };
console.log({ key1, key2 }); // { key1 : 'a', key2 : 'b' }

//defalut value
const { key1, key2, key3 = 'c' } = { key1 : 'a', key2 : 'b' };
console.log({ key1, key2, key3 }); // { key1 : 'a', key2 : 'b', key3 : 'c' }
  • Array.prototype.filter ๋ฉ”์†Œ๋“œ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋Œ€์ƒ ๋ฐฐ์—ด(todos)์„ ์ˆœํšŒํ•˜๋ฉฐ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋Œ€์ƒ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๋ฐ›๋Š”๋‹ค. ์ด๋•Œ ํ•„์š”ํ•œ ํ”„๋กœํผํ‹ฐ(key)(completed ํ”„๋กœํผํ‹ฐ)๋งŒ์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
const todos = [
  { id: 1, content: 'HTML', completed: true },
  { id: 2, content: 'CSS', completed: false },
  { id: 3, content: 'JS', completed: false }
];

// todos ๋ฐฐ์—ด์˜ ์š”์†Œ์ธ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ completed ํ”„๋กœํผํ‹ฐ๋งŒ์„ ์ถ”์ถœํ•œ๋‹ค.
const completedTodos = todos.filter(({ completed }) => completed);
console.log(completedTodos); // [ { id: 1, content: 'HTML', completed: true } ]
  • ์ค‘์ฒฉ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.
const person = {
  name: 'Sony',
  address: {
    zipCode: '06219',
    city: 'Seoul'
  }
};

const { address: { city } } = person;
console.log(city); // 'Seoul'

Reference