JavaScript ES6의 객체 및 배열 분해
객체를 파괴
기본값
const product = {
name: 'Mac M1 256GB',
price: 1100,
description: 'This is a Mac M1 Chip with 256GB SSD',
warrenty: '1 Year',
stock: 11,
};
const { name, price, featured = false } = product;
console.log(`${name}, ${price}, ${featured}`); // Mac M1 256GB, 1100, false
다른 변수 이름
const product = {
name: 'Mac M1 256GB',
price: 1100,
description: 'This is a Mac M1 Chip with 256GB SSD',
warrenty: '1 Year',
stock: 11,
};
const { name: productName, price: productPrice, featured: isProductFeatured = false } = product;
console.log(`${productName}, ${productPrice}, ${isProductFeatured}`); // Mac M1 256GB, 1100, false
중첩된 객체
const product = {
name: 'Mac M1 256GB',
price: 1100,
description: 'This is a Mac M1 Chip with 256GB SSD',
warrenty: '1 Year',
stock: 11,
tech: {
os: 'iOS',
logo: './mac_m1.png',
production_year: '2018',
production_country: 'USA',
},
};
const {
name: productName,
tech: { os: productOS, logo: productLogo, expired = false },
} = product;
console.log(`${productName}, ${productOS}, ${productLogo}, ${expired}`); // Mac M1 256GB, iOS, ./mac_m1.png, false
const product = {
name: 'Mac M1 256GB',
price: 1100,
description: 'This is a Mac M1 Chip with 256GB SSD',
warrenty: '1 Year',
stock: 11,
tech: {
os: 'iOS',
logo: './mac_m1.png',
production_year: '2018',
production_country: 'USA',
},
};
const { os: productOS, logo: productLogo, expired = false } = product.tech;
console.log(`${productOS}, ${productLogo}, ${expired}`); // iOS, ./mac_m1.png, false
구조 파괴 배열
기본값
const phones = ['iPhone 8', 'Samsung S22', 'iPhone XS'];
const [firstPhone, secondPhone, thirdPhone, fourthPhone = 'Oppo Ultra Max'] = phones;
console.log(`${firstPhone}, ${secondPhone}, ${thirdPhone}, ${fourthPhone}`); // iPhone 8, Samsung S22, iPhone XS, Oppo Ultra Max
요소 건너뛰기
const phones = ['iPhone 8', 'Samsung S22', 'iPhone XS', 'Oppo New Release', 'iPhone 11', 'iPhone 12', 'iPhone 13'];
const [firstPhone, , thirdPhone, fourthPhone = 'Oppo Ultra Max', , , seventhPhone] = phones;
console.log(`${firstPhone}, ${thirdPhone}, ${fourthPhone}, ${seventhPhone}`); // iPhone 8, iPhone XS, Oppo New Release, iPhone 13
중첩 배열
const phones = ['iPhone 8', 'Samsung S22', ['Unnamed Phone 1', 'Unnamed Phone 2', 'Unnamed Phone 3']];
const [, , [firstNestedPhone, secondNestedPhone, thirdNestedPhone]] = phones;
console.log(`${firstNestedPhone}, ${secondNestedPhone}, ${thirdNestedPhone}`); // Unnamed Phone 1, Unnamed Phone 2, Unnamed Phone 3
const phones = ['iPhone 8', 'Samsung S22', ['Unnamed Phone 1', 'Unnamed Phone 2', ['Clear Phone 1', 'Clear Phone 2']]];
const [, , [firstNestedPhone, , [firstDeepNestedPhone, secondDeepNestedPhone]]] = phones;
console.log(`${firstNestedPhone}, ${firstDeepNestedPhone}, ${secondDeepNestedPhone}`); // Unnamed Phone 1, Clear Phone 1, Clear Phone 2
나머지 매개변수
const phones = ['iPhone 8', 'Samsung S22', 'iPhone XS', 'Google Pixel 6'];
const [firstPhone, ...restPhones] = phones;
console.log(`${restPhones}`); // Samsung S22,iPhone XS,Google Pixel 6
스와핑
let firstPhone = 'iPhone 13 Pro Max',
secondPhone = 'iPhone 12 Pro Max';
[firstPhone, secondPhone] = [secondPhone, firstPhone];
console.log(`${firstPhone}, ${secondPhone}`); // iPhone 12 Pro Max, iPhone 13 Pro Max
Reference
이 문제에 관하여(JavaScript ES6의 객체 및 배열 분해), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/anasnmu/object-and-array-destructuring-in-javascript-es6-7lh텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)