LogoDuyệtSr. Data Engineer
HomeAboutPhotosInsightsCV

Footer

Logo

Resources

  • Rust Tiếng Việt
  • /archives
  • /series
  • /tags
  • Status

me@duyet.net

  • About
  • LinkedIn
  • Resume
  • Projects

© 2026 duyet.net | Sr. Data Engineer

Cách sử dụng Destructuring trong Javascript ES6

Note: This post is over 10 years old. The information may be outdated.

Destructuring là chức năng mới trong ES6 của Javascript. Giúp extract dữ liệu (có thể lồng nhau) từ Array hoặc Object.

1. Object destructuring

const obj = { first: 'Jane', last: 'Doe' }
const { first: f, last: l } = obj
// f = 'Jane'; l = 'Doe'

// {prop} is short for {prop: prop}
const { first, last } = obj
// first = 'Jane'; last = 'Doe'

2. Array destructuring

Destructuring giá trị từ mảng

const iterable = ['a', 'b']
const [x, y] = iterable
// x = 'a'; y = 'b'

Destructuring cũng giúp xử lý giá trị được trả về

const [all, year, month, day] = /^(\d\d\d\d)-(\d\d)-(\d\d)$/.exec('2999-12-31')

3. Khi nào cần sử dụng destructuring

Cơ bản, destructuring có thể được sử dụng ở 1 số trường hợp sau:

// Khai báo giá trị
const [x] = ['a'];
let [x] = ['a'];
var [x] = ['a'];

// Gán giá trị
[x] = ['a'];

// Định nghĩa các tham số
function f([x]) { ··· }
f(['a']);

Sử dụng với vòng lặp for-of

const arr1 = ['a', 'b']
for (const [index, element] of arr1.entries()) {
  console.log(index, element)
}
// Output:
// 0 a
// 1 b

const arr2 = [
  { name: 'Duyệt', age: 21 },
  { name: 'Hoa', age: 19 },
]
for (const { name, age } of arr2) {
  console.log(name, age)
}
// Output:
// Duyệt 21
// Hoa 19

4. Các Patterns

Một số patterns sử dụng sâu hơn chức năng này:

4.1 Pick what you need

Chỉ bóc tách lấy giá trị trong Object mà bạn cần

const { x: x } = { x: 7, y: 3 } // x = 7

// Array
const [x, y] = ['a', 'b', 'c'] // x='a'; y='b';

// Hoặc khó hơn, và "sâu" hơn như thế này
const obj = { a: [{ foo: 123, bar: 'abc' }, {}], b: true }
const {
  a: [{ foo: f }],
} = obj // f = 123

4.2 Object patterns coerce values to objects

Pattern này ép giá trị nguồn (bên phải) thành object trước, khó để giải thích cái này, bạn xem ví dụ bên dưới

const { length: len } = 'abc' // len = 3
const { toString: s } = 123 // s = Number.prototype.toString

4.3 Array patterns work with iterables

// Strings are iterable:
const [x, ...y] = 'abc' // x='a'; y=['b', 'c']

4.4 Sử dụng với Generator function (yield)

function* allNaturalNumbers() {
  for (let n = 0; ; n++) {
    yield n
  }
}

const [x, y, z] = allNaturalNumbers() // x=0; y=1; z=2

4.5 Default values

const [x = 3, y] = [] // x = 3; y = undefined
const { foo: x = 3, bar: y } = {} // x = 3; y = undefined
const [x = 1] = [undefined] // x = 1
const { prop: y = 2 } = { prop: undefined } // y = 2

const [x = 3, y = x] = [] // x=3; y=3
const [x = 3, y = x] = [7] // x=7; y=7
const [x = 3, y = x] = [7, 2] // x=7; y=2

// Khó hơn
const [{ prop: x } = {}] = []

4.6 Khuyết tham số

const [, , x, y] = ['a', 'b', 'c', 'd'] // x = 'c'; y = 'd'

4.7. Load module Node.js

const { Loader, main } = require('toolkit/loader')

5. Tham khảo

  • http://exploringjs.com/es6/ch_destructuring.html
  • https://gist.github.com/mikaelbr/9900818
May 27, 2016·10 years ago
|Javascript|
Node.jsEs6JavascriptTutorial
|Edit|

Related Posts

Resting và Spreading JavaScript Objects

Resting và spreading càng ngày được ưa chuộng vì sự tiện lợi của nó, sau đây là 7 tricks với JavaScript objects.

Mar 27, 2019·7 years ago
Read more

Javascript Weekly #7

Javascript Weekly #7 tuần này: giới thiệu HyperTerm - Command Line mạnh mẽ dựa trên công nghệ của Web, Node.js, Howler.js là một thư viện Audio nhiều tính năng, hay cách xây dựng ứng dụng Desktop sử dụng Electron và React, khám phá 2 chức năng mới trong ES6, ...

Jul 22, 2016·10 years ago
Read more

ES6 - ép sử dụng tham số trong function

Các tham số function trong Javascript thực ra không bắt buộc, Javascript chỉ kiểm tra khi nó được sử dụng đến. Một số trường hợp ta muốn bắt buộc người sử dụng hàm phải điền tham số này khi gọi hàm. Thủ thuật sau sử dụng chức năng default param trong ES6

May 27, 2016·10 years ago
Read more

ES6 - swap (hoán đổi) nhanh 2 biến số

ES6 có chức năng destructuring có khá nhiều công dụng. Thủ thuật sau giúp hoán đổi giá trị 2 biến bằng cách destructuring.

May 27, 2016·10 years ago
Read more
On this page
  • 1. Object destructuring
  • 2. Array destructuring
  • 3. Khi nào cần sử dụng destructuring
  • 4. Các Patterns
  • 5. Tham khảo
On this page
  • 1. Object destructuring
  • 2. Array destructuring
  • 3. Khi nào cần sử dụng destructuring
  • 4. Các Patterns
  • 5. Tham khảo