Destructuring
Clean Code - Part 2
1. TypeScript - Tuple & Array π©βπ»
1. Assignment
JavaScript μλ Tuple μ΄ μ‘΄μ¬νμ§ μκΈ° λλ¬Έμ TypeScript μμλ κ·Έλ₯ JavaScript Array κ° λ³΅μμ Types λ₯Ό λ΄μ μ μλ€λ κ²μ μ΄μ©ν΄ Tuple λ‘ μ¬μ©νκΈ° λλ¬Έμ Tuple κ³Ό Array λ λμΌνκ² μ¬μ©μ΄ κ°λ₯νλ€.
const [foo, bar] = ["λ무", "νΈκ·ΈμνΈ"]
console.log(foo) // λ무
console.log(bar) // νΈκ·ΈμνΈ
μ μ½λλ λ€μκ³Ό κ°λ€.
const foo = "λ무"
const bar = "νΈκ·ΈμνΈ"
console.log(foo) // λ무
console.log(bar) // νΈκ·ΈμνΈ
λ¬Όλ‘ μ΄λ κ² μ¬μ©ν μλ μλ€.
const some = ["λ무", "νΈκ·ΈμνΈ"]
const [foo, bar] = some
console.log(foo) // λ무
console.log(bar) // νΈκ·ΈμνΈ
λν default value λ₯Ό μ§μ ν μ μλ€.
const [foo = "νΈλ", bar = "μ¬κ³Ό"] = ["λ무"]
console.log(foo) // λ무
console.log(bar) // μ¬κ³Ό
2. XOR Swap
JavaScript μ Array λ₯Ό μ΄μ©ν XOR Swap μ μ§μνλ―λ‘ μλμ κ°μ΄ μ½κ² μ²λ¦¬ν μ μλ€
let [foo, bar] = ["λ무", "νΈκ·ΈμνΈ"]
;[foo, bar] = [bar, foo]
console.log(foo) // νΈκ·ΈμνΈ
console.log(bar) // λ무
;[foo, bar] = [bar, foo]
μ μ£Όλͺ©νμ.IIFE
μ λ§μ°¬κ°μ§λ‘ μμ ν μ½λλ₯Ό μν΄ μμ λΆλΆμ;
λ₯Ό λ£μ΄μ£Όμ.
3. Return Types
μ¬μ€ Tuple μμ²΄κ° Array μ΄λ―λ‘ μλμ κ°μ΄ λ°λ‘ λΆν΄ν΄ ν λΉμν¬ μ μλ€.
const [first, last] = "Harry Potter".split(" ")
console.log(first) // Harry
console.log(last) // Potter
Assignment μμ default value λ₯Ό μ μνμλ€. λ°λλ‘ μΌλΆ κ°μ 무μνλ κ² μμ κ°λ₯νλ€.
const foo = () => ["μ¬κ³Ό", "λ°°", "λ©λ‘ ", "λΈκΈ°"]
const [apple, , melon, strawberry] = foo()
console.log(apple, melon, strawberry) // μ¬κ³Ό λ©λ‘ , λΈκΈ°
, ,
λ₯Ό μ¬μ©ν΄ μ리λ₯Ό λ§λ€μ΄ μ£Όλ κ·Έλ₯ 무μνλ©΄ λλ€.
4. Spread Operator
const [fruits1, ...fruits2] = foo()
console.log(fruits1) // μ¬κ³Ό
console.log(fruits2) // ["λ°°", "λ©λ‘ ", "λΈκΈ°"]
Spread Operator μ ν¨κ» μ¬μ©νλ κ² μμ κ°λ₯νλ€.
2. TypeScript - Object
1. Assignment
Destructuring μ΄ κ°μ₯ λΉμ λ°νλ κ³³μ΄ λ°λ‘ Object λ€.
const lecture1 = {
subject: 'English',
students: 40,
level: 'Intermediate',
discount: false
}
const { level, students } = lecture1
console.log(students) // 40
console.log(level) // Intermediate
Object λ Key-Value
Types μ¬μ Array μ λ¬λ¦¬ λΆνμν κ°μ λν΄ , ,
μ κ°μ΄ λΉ κ³΅κ°μ΄λ μμλ₯Ό κ³ λ €ν νμλ μμ΄ μμ λ‘κ² λΆν΄κ°
κ°λ₯νλ€.
μ μΈκ³Ό ν λΉμ λΆλ¦¬νλ κ² μμ κ°λ₯νλ€.
let students, level
;( { students, level } = lecture1 )
console.log(students) // 40
console.log(level) // Intermediate
XOR Swap μμ λ³Έ κ²μ²λΌ μ΄ κ²½μ°λ IIFE μ λ§μ°¬κ°μ§λ‘ μμ λΆλΆμ
;
λ₯Ό λ£μ΄ μ£Όμ΄μΌ νλ€. λ¨, μμμλ
;[foo, bar] = [bar, foo]
μ΄λ κ²λ§ ν΄μ€λ μλνλ κ²κ³Ό λ¬λ¦¬ μ¬κΈ°μλ{ }
μ΄ block μ΄ Code Chunk κ° μλ Object λΌλ κ²μ λͺ μνμ§ μμΌλ©΄ μ½λ μλ¬κ° λ°μνλ€.
λ°λΌμ;( { students, level } = lecture1 )
μ κ°μ΄;( )
λ‘ κ°μΈμ€μΌνλ€.
2. Assignment with NewName
κ°μ ν λΉν λ λ€μ Types λ₯Ό μ μνλ― : variableName
λ₯Ό λΆμ¬μ£Όλ©΄ ν΄λΉ μ΄λ¦μ λ³μμ ν λΉλλ€.
const { students, level: lectureLevel } = lecture1
console.log(students) // 40
console.log(lectureLevel) // Intermediate
λ§μ°¬κ°μ§λ‘ default value λ₯Ό μ§μ ν μ μλ€.
const { students = 30, expire = false } = lecture1
console.log(students) // 40
console.log(expire) // false
3. Function Parameters
Destructuring μ΄ κ°μ₯ μ μ©ν κ³³μ΄ λ°λ‘ ν¨μμ Parameters λ€.
const draw = (width?: number, height?: number, color: string = 'white') => {
if (width === undefined || height === undefined) return
return `The color of this square is ${color}, and area is ${width * height}.`
}
console.log(draw(5, 7, 'blue'))
console.log(draw(3, 6))
The color of this square is blue, and area is 35.
The color of this square is white, and area is 18.
μ ν¨μμ κ²½μ° Parameters μ κ°μ κ°κ° μ€μΌνλ€. λ¬Έμ λ μ°λ¦¬λ λλΆλΆ μ λ° μμ λ°μ΄ν°λ νλμ κ°μ²΄λ‘ λ€λ£¬λ€λ κ²μ΄λ€. μ¦, λμ΄μ λμ΄, μμκ³Ό κ°μ μ 보λ μλμ κ°μ΄ μ‘΄μ¬ν κ²μ΄λ€.
const square1: Square = {
width: 5,
height: 7,
color: 'blue'
}
const square2: Square = {
width: 3,
height: 6
}
λ°λΌμ μ΄λ κ² μ§μ λΆν΄ν΄μ Arguments λ‘ λμ§κ±°λ
draw(square1.width, square1.height, square1.color)
μμ μλμ κ°μ΄ ν¨μκ° Parameters λ‘ Object λ₯Ό λ°λλ‘ ν΄μΌνλ€.
const draw = (square: Square) => {
if (square.width === undefined || square.height === undefined) return
return `The color of this square is ${square.color}, and area is ${square.width * square.height}.`
}
draw(square1)
μ΄λ€ λ°©λ²μ μ¬μ©νλ square.
μ κ°μ μμΌλ‘ Dot Syntax λ₯Ό μ¬μ©ν΄μΌλ§ νκ³ μ½λκ° κΈΈμ΄μ§λ€. μ¬μ€ λλΆλΆ λ€λ₯Έ μΈμ΄μμλ μ΄λ κ² κ°μ²΄λ‘
λ€λ£¬λ€. νμ§λ§ TypeScript μμλ Destructuring μ μ¬μ©ν΄ κ°μ²΄λ‘ λ€λ£¨λ©΄μλ κ°μ²΄κ° μλ κ²μ²λΌ κ°μ
μ¬μ©ν μ μλλ‘ ν¨μλ₯Ό μ μν μ μλ€.
const draw = ({ width, height, color = 'white' }: Square) => {
if (width === undefined || height === undefined) return
return `The color of this square is ${color}, and area is ${width * height}.`
}
console.log(draw(square1))
console.log(draw(square2))
The color of this square is blue, and area is 35.
The color of this square is white, and area is 18.
Arguments λ§ λ³΄λ©΄
draw(_:)
ν¨μμ κ°μ²΄λ₯Ό λμ Έ λ£μ΄ μλμν€μ§λ§, ν¨μ λ΄λΆμμ λ°λ Parameters λ Destructuring λμ΄darw(_:_:_:)
ν¨μμΈ κ²μ²λΌ μλνλ€.
4. Nested Object
TypeScript μ Destructuring μ κ°λ ₯ν μ μ μ€μ²©λ ꡬ쑰 μμ μ½κ² νμ΄λΌ μ μλ€λ κ²μ΄λ€. μ΄κ²μ ν¨μμ Parameters λ₯Ό νμ΄λ΄λ κ² λ§νΌ μ μ©νλ€.
const styles = {
size: {
width: 300,
height: 200
},
colors: {
fontColor: 'red',
bgColor: 'blue'
}
}
μ€μ²© ꡬ쑰λ₯Ό νμ΄λ΄λ λ°©λ²μ Assignment with NewName μ κ°μ΄ : variableName
μ μ΄μ©νλλ°
μ΄λ μ€μ²© ꡬ쑰λ₯Ό νννκΈ° μν΄ {originalProperty: variableName}
ννλ‘ λ§λ€μ΄ μ£Όλ κ²μ΄λ€. μλ μμ λ₯Ό 보μ.
const { size: { width, height }, colors: { fontColor, bgColor } } = styles
console.log(width) // 300
console.log(height) // 200
console.log(fontColor) // red
console.log(bgColor) // blue
μ€μ²©λ Object ꡬ쑰μ μ‘΄μ¬νλ Properties λ₯Ό μ λΆ 1μ°¨μμΌλ‘ λΆν΄μμΌ°λ€.
μ’ λ 볡μ‘ν μμ λ₯Ό μ΄ν΄λ³΄μ.
const metadata = {
title: "Scratchpad",
translations: [
{
locale: "de",
localizationTags: [],
lastEdit: "2014-04-14T08:43:37",
url: "/de/docs/Tools/Scratchpad",
title: "JavaScript-Umgebung",
},
],
url: "/en-US/docs/Tools/Scratchpad",
}
metadata
μμ title, translations μμͺ½μ title 2κ°λ§ λΆν΄νκ³ μΆλ€. κ·Έλ°λ° κΈ°μ‘΄μ title μ englishTitle
μ΄λΌλ
λ³μλ‘, κ·Έλ¦¬κ³ translations μμͺ½μ title μ localTitle
μ΄λΌλ λ³μλ‘ λΆν΄νκ³ μ νλ€.
λ°λΌμ translations μμͺ½μ title μ [ ]
μμͺ½μ { }
μμ μλ€. μ¦, [ { } ]
μ μ κ·Όν΄μΌνλ€. μ΄ κ΅¬μ‘°λ₯Ό κ·Έλλ‘ λ°λΌκ°λ³΄μ.
const {
title: englishTitle,
translations: [
{
title: localTitle
}
]
} = metadata
console.log(englishTitle) // Scratchpad
console.log(localTitle) // JavaScript-Umgebung
5. Iterating Over a Object in an Array
Object μ μ€μ²© 보λ€λ μ½μ§λ§, μ€μ λ‘ λ λ§μ΄ μ¬μ©λλ κ²½μ°λ€.
type Student = {
name: string,
age: number,
grade: string
}
const studentInfo = [
{name: 'David', age: 17, grade: 'junior'},
{name: 'Julia', age: 18, grade: 'sophomore'},
{name: 'Michael', age: 18, grade: 'sophomore'},
]
studentInfo.map((student, index) => (
console.log(`Student ${index + 1}: ${student.name}`)
))
Destructuring μ΄ μλ€λ©΄ μ΄λ κ² μ κ·Όνκ² μ§λ§, Destructuring μ μ¬μ©νλ©΄ μλμ κ°μ΄ μ κ·Όν μ μλ€.
studentInfo.map(({name}, index) => (
console.log(`Student ${index + 1}: ${name}`)
))
3. Swift - Tuple π©βπ»
1. Assignment
let (foo, bar) = ("λ무", "νΈκ·ΈμνΈ")
print(foo) // λ무
print(bar) // νΈκ·ΈμνΈ
μ μ½λλ λ€μκ³Ό κ°λ€.
let foo = "λ무"
let bar = "νΈκ·ΈμνΈ"
print(foo) // λ무
print(bar) // νΈκ·ΈμνΈ
λ¬Όλ‘ μ΄λ κ² μ¬μ©ν μλ μλ€.
let some = ("λ무", "νΈκ·ΈμνΈ")
let (foo, bar) = some
print(foo) // λ무
print(bar) // νΈκ·ΈμνΈ
2. XOR Swap
Swift μ Tuple μ μ΄μ©ν XOR Swap μ μ§μνλ―λ‘ μλμ κ°μ΄ μ½κ² μ²λ¦¬ν μ μλ€.
var (foo, bar) = ("λ무", "νΈκ·ΈμνΈ")
(foo, bar) = (bar, foo)
print(foo) // νΈκ·ΈμνΈ
print(bar) // λ무
3. Return Types
λν Array λ₯Ό Tuple λ‘ λ°ννλ ν¨μλ₯Ό λ§λ€μ΄ λ€μκ³Ό κ°μ΄ νμ©ν μ μλ€.
func splitName(_ name: String) -> (String, String) {
let parts = name.components(separatedBy: " ")
return (parts[0], parts[1])
}
let (first, last) = splitName("Harry Potter")
print(first) // Harry
print(last) // Potter
4. Swift - Dictionary
1. Iterating Over a Dictionary
// `Planet` Type
let Planet = [
1: "Mercury",
2: "Venus",
3: "Earth",
4: "Mars",
5: "Jupiter",
6: "Saturn",
7: "Uranus",
8: "Neptune"
]
Planet.sorted(by: <).forEach { (number, name) in
print(#"\#(number)λ²μ§Έ μμ±μ "\#(name)" μ
λλ€."#)
}
2. Iterating Over a Dictionary in an Array
Array μ체λ Destructuring ν κ²μ΄ μλ€. νμ§λ§ Array μμ Dictionary κ° λ€μ΄μλ ννλΌλ©΄, μ΄ Dictionary λ₯Ό Destructuring ν μ μλ€.
// `[Planet]` Type
let Planet = [
[1: "Mercury"],
[2: "Venus"],
[3: "Earth"],
[4: "Mars"],
[5: "Jupiter"],
[6: "Saturn"],
[7: "Uranus"],
[8: "Neptune"]
]
Planet.flatMap { $0 }.forEach { (number, name) in
print(#"\#(number)λ²μ§Έ μμ±μ "\#(name)" μ
λλ€."#)
}
Reference
- βDestructuring assignment.β MDN Web Docs. Apr. 18, 2023, MDN - Destructuring assignment.
- Paul Hudson. βWhat is destructuring?.β Hacking With Swift. May. 28, 2019, Swift Destructuring.