Mảng đối tượng javascript

Tiếp tục về loạt bài JavaScript, hôm nay chúng ta sẽ tìm hiểu về

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
2 và
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
3 trong
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
4. ES6 cung cấp cho chúng ta những cách thức mới để làm việc với
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
2 và
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
3, đặc biệt là phạm vi dữ liệu trong hai đối tượng trên. Bài viết sẽ bao gồm ba nội dung là.
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
7,
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
8 và
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
9

Nhiệm vụ phá hủy

        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
0 cho phép chúng tôi giới hạn phạm vi bộ cục bộ của các trường trong một đối tượng và đưa chúng ra để sử dụng. Ví dụ chúng ta có một đối tượng là
        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
1 và đối tượng này có 4 trường, tuy nhiên chúng ta chỉ muốn sử dụng 2 trường trong 4 trường đó và chúng ta sẽ giới hạn bằng
        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
0 như sau

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
1

Dòng lệnh

        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
3 đã lấy hai trường
        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
4 và
        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
5 và tạo ra 2 biến cục bộ tương ứng là
        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
4 và
        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
5, do đó hai biến này có thể thay đổi được [chỉ giá trị của hai biến cục bộ thay đổi còn giá trị của hai biến . Ví dụ

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
8

Các bạn nên lưu ý rằng chúng ta sử dụng từ khóa

        let lordify = [{firstname}] => {
            console.log[`${firstname} of Canterbury`]
        }
         lordify[regularPerson] // Bill of Canterbury
9 cho
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
00 nên giá trị có thể thay đổi được, nếu chúng ta sử dụng từ khóa
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
01 thì hai giá trị của biến cục bộ này sẽ không thể thay đổi. Ví dụ

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
2

Chúng ta cũng có thể

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
00 cho tham số của một chức năng. Chúng ta xem xét ví dụ sau

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury

Thay thế cho việc sử dụng cú pháp

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
03 [regularPerson. firstname] để truy cập vào đối tượng, ta sẽ sử dụng
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
00 để lấy giá trị mong muốn từ
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
05

________số 8

Ngoài ra

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
06 còn được áp dụng cho
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
3. Ví dụ chúng ta muốn lấy giá trị của phần tử đầu tiên của một
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
08

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
0

Một hướng khác, nếu chúng ta muốn lấy giá trị của phần tử thứ 2 hoặc thứ 3 như sau

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
3

Chúng ta sẽ thay thế các vị trí của các phần tử không cần thiết bằng dấu

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
09, từ đó chúng ta sẽ lấy được giá trị của phần tử tương ứng. Chú ý _______ 330 là cách viết gọn của _______ 331, tương tự
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
32 là cách viết gọn của
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
33

Tăng cường đối tượng theo nghĩa đen

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
34 là một cách mới để chúng ta tương tác với các
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
35. Với tính năng tăng cường đối tượng theo nghĩa đen, chúng ta có thể kết hợp các biến
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
36 và đưa chúng vào trong một đối tượng. Ví dụ

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
2

Như vậy,

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
37 và
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
38 bây giờ là chìa khóa của người đối tượng

Mặt khác, chúng ta còn có thể tạo hoặc tái cấu trúc đối tượng

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
39 với đối tượng nâng cao theo nghĩa đen

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
6

Chú ý ở trên chúng ta sử dụng từ khóa

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
20 để truy cập khóa của đối tượng

Khi định nghĩa một phương thức đối tượng, chúng ta không cần thiết phải sử dụng từ khóa

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
21

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
9

Như vậy

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
34 đã cho phép chúng ta kéo các biến toàn cục
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
36 vào trong một
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
35 và rút ngắn mã hơn với việc không cần thiết sử dụng từ khóa
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
21

Toán tử lây lan

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
26 is dấu ba chấm [. ] và cú pháp này sẽ giúp chúng ta xử lý một đối tượng hoặc mảng theo một vài cách mới. Đầu tiên, toán tử trải rộng cho phép chúng ta tổng hợp nội dung của một mảng [mảng]. Ví dụ, chúng ta sẽ có hai mảng và tổ hợp chúng thành một mảng thứ ba

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
80

Tất cả các phần tử của đỉnh núi và hẻm núi đã được đưa vào một mảng mới là

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
27

Chúng ta sẽ xem xét một vấn đề sau, giả sử chúng ta có một mảng

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
28 như ví dụ trước và chúng ta muốn lấy giá trị của phần tử cuối cùng trong mảng sử dụng
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
00 và theo cách lấy phần tử đầu tiên. Chúng ta có thể xử lý vấn đề này như sau

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
81

Tuy nhiên, theo cách trên, bản thân mảng

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
28 đã bị đảo ngược [đảo ngược] tức là mảng
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
28 đã bị thay đổi. Khi sử dụng
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
9, chúng ta không thể thay đổi mảng gốc ban đầu, chúng ta có thể tạo ra một bản sao của mảng và đảo ngược bản sao này để đảm bảo mảng ban đầu không bị thay đổi

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
82

Chúng ta cũng có thể sử dụng

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
9 để lấy một hoặc một vài phần tử trong mảng

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
83

Chúng ta còn có thể sử dụng

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
9 cho tập hợp tham số của
        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
21 như một mảng. Ví dụ

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
84

Ngoài ra, chúng ta cũng có thể sử dụng

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
9 cho đối tượng. Cách sử dụng tương tự như mảng

        let lordify = regularPerson => {
            console.log[`${regularPerson.firstname} of Canterbury`]
        }
        
        let regularPerson = {
            firstname: "Bill",
            lastname: "Wilson"
        }
        
        lordify[regularPerson] // Bill of Canterbury
85

Vì vậy, chúng ta đã xác định được một số cách sử dụng cú pháp cú pháp trên. Nếu có điều gì còn sót lại, các bạn hãy để lại bình luận phía dưới để bài viết được hoàn thiện hơn. Hẹn gặp lại các bạn ở bài viết lần sau

Chủ Đề