Mảng JavaScript là một biến duy nhất được sử dụng để lưu trữ các phần tử hoặc một nhóm giá trị. Có thể thêm bớt phần tử trong mảng ở bất kỳ vị trí nào. Trong bài viết này, chúng ta sẽ thảo luận về các cách khác nhau để loại bỏ các phần tử khỏi mảng.
Có nhiều phương pháp được sử dụng để xóa các phần tử khỏi mảng JavaScript sẽ được thảo luận bên dưới.
- Hàm bật [] trong JavaScript. Phương thức này dùng để loại bỏ các phần tử ở cuối mảng
- Hàm shift[] trong JavaScript. Phương thức này được sử dụng để loại bỏ các phần tử từ đầu của một mảng
- Hàm nối JavaScript []. Phương pháp này được sử dụng để xóa các phần tử khỏi chỉ mục cụ thể của một mảng
- Hàm bộ lọc JavaScript[]. Phương pháp này được sử dụng để loại bỏ các phần tử theo cách lập trình
Ghi chú. Có một số phương thức khác được tạo bởi các phương thức sẵn có của JavaScript
Các ví dụ dưới đây minh họa các phương pháp xóa phần tử khỏi mảng JavaScript
Loại bỏ các phần tử Array bằng cách sử dụng phương thức pop[]. Phương thức này dùng để xóa phần tử cuối cùng của mảng và trả về phần tử đã xóa. Hàm này giảm độ dài của mảng xuống 1.
ví dụ 1.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 05
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 07
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop0
Removed element: shift Remaining elements: splice, filter, pop1
Removed element: shift Remaining elements: splice, filter, pop2______73
Removed element: shift Remaining elements: splice, filter, pop4
Removed element: shift Remaining elements: splice, filter, pop5
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop7
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop9
Removed element: shift Remaining elements: splice, filter, pop6
Original array: pop, splice, filter, shift Array Length: 081
Original array: pop, splice, filter, shift Array Length: 082
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop2____185
Removed element: shift Remaining elements: splice, filter, pop2____73
Original array: pop, splice, filter, shift Array Length: 088
Removed element: shift Remaining elements: splice, filter, pop2____180____181
Original array: pop, splice, filter, shift Array Length: 082
Removed element: shift Remaining elements: splice, filter, pop2____180____185
Original array: pop, splice, filter, shift Array Length: 086
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 070
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 09
ví dụ 2.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 074
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 077
Original array: pop, splice, filter, shift Array Length: 081
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop7
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop9
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop5
Original array: pop, splice, filter, shift Array Length: 035
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____539
Original array: pop, splice, filter, shift Array Length: 080
Original array: pop, splice, filter, shift Array Length: 081
Original array: pop, splice, filter, shift Array Length: 082
Original array: pop, splice, filter, shift Array Length: 04
_______04____585
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 087
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop2
Original array: pop, splice, filter, shift Array Length: 091
Removed element: shift Remaining elements: splice, filter, pop2
Original array: pop, splice, filter, shift Array Length: 093
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____698
Original array: pop, splice, filter, shift Array Length: 099
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 0
Loại bỏ các phần tử Array bằng cách sử dụng phương thức shift[]. Phương thức này dùng để loại bỏ phần tử đầu tiên của mảng và giảm kích thước của mảng ban đầu đi 1.
Thí dụ.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop23
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop25
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop0
Removed element: shift Remaining elements: splice, filter, pop1
Removed element: shift Remaining elements: splice, filter, pop2______73
Removed element: shift Remaining elements: splice, filter, pop4
Removed element: shift Remaining elements: splice, filter, pop5
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop7
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop9
Removed element: shift Remaining elements: splice, filter, pop6
Original array: pop, splice, filter, shift Array Length: 081
Original array: pop, splice, filter, shift Array Length: 082
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop2____012
Removed element: shift Remaining elements: splice, filter, pop2
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 015
Removed element: shift Remaining elements: splice, filter, pop2____180____181
Original array: pop, splice, filter, shift Array Length: 019
Removed element: shift Remaining elements: splice, filter, pop2____180____185
Original array: pop, splice, filter, shift Array Length: 086
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 070
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Removed element: shift Remaining elements: splice, filter, pop
Loại bỏ các phần tử Array bằng cách sử dụng phương thức splice[]. Phương thức này được sử dụng để sửa đổi nội dung của một mảng bằng cách loại bỏ các phần tử hiện có và/hoặc bằng cách thêm các phần tử mới. Để xóa các phần tử bằng phương thức splice[], bạn có thể chỉ định các phần tử theo nhiều cách khác nhau.
ví dụ 1. Sử dụng lập chỉ mục của phương thức nối để xóa các phần tử khỏi mảng JavaScript.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 031
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop0
Removed element: shift Remaining elements: splice, filter, pop1
Removed element: shift Remaining elements: splice, filter, pop2______73
Removed element: shift Remaining elements: splice, filter, pop4
Removed element: shift Remaining elements: splice, filter, pop5
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop7
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop9
Removed element: shift Remaining elements: splice, filter, pop6
Original array: pop, splice, filter, shift Array Length: 081
Original array: pop, splice, filter, shift Array Length: 082
Original array: pop, splice, filter, shift Array Length: 04
_______72____049
Removed element: shift Remaining elements: splice, filter, pop2____73
Original array: pop, splice, filter, shift Array Length: 052
Removed element: shift Remaining elements: splice, filter, pop2____180____181
Original array: pop, splice, filter, shift Array Length: 056
Removed element: shift Remaining elements: splice, filter, pop2____180____185
Original array: pop, splice, filter, shift Array Length: 086
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 070
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 08
ví dụ 2. Sử dụng giá trị của phương thức mối nối để xóa các phần tử khỏi mảng JavaScript.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 031
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop0
Removed element: shift Remaining elements: splice, filter, pop1
Removed element: shift Remaining elements: splice, filter, pop2______73
Removed element: shift Remaining elements: splice, filter, pop4
Removed element: shift Remaining elements: splice, filter, pop5
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop7
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop9
Removed element: shift Remaining elements: splice, filter, pop6
Original array: pop, splice, filter, shift Array Length: 081
Original array: pop, splice, filter, shift Array Length: 082
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop2____085
Removed element: shift Remaining elements: splice, filter, pop2
Original array: pop, splice, filter, shift Array Length: 087
Original array: pop, splice, filter, shift Array Length: 088
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 090
Original array: pop, splice, filter, shift Array Length: 091
Original array: pop, splice, filter, shift Array Length: 092
Original array: pop, splice, filter, shift Array Length: 093
Removed element: shift Remaining elements: splice, filter, pop7
Original array: pop, splice, filter, shift Array Length: 095
Original array: pop, splice, filter, shift Array Length: 096
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 098
Original array: pop, splice, filter, shift Array Length: 096
Original array: pop, splice, filter, shift Array Length: 080
Original array: pop, splice, filter, shift Array Length: 081
Original array: pop, splice, filter, shift Array Length: 056
Original array: pop, splice, filter, shift Array Length: 096
Original array: pop, splice, filter, shift Array Length: 080
Original array: pop, splice, filter, shift Array Length: 085
Original array: pop, splice, filter, shift Array Length: 086
_______091____188
Removed element: shift Remaining elements: splice, filter, pop2____188
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 070
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 08
ví dụ 3. Sử dụng phương thức mối nối để xóa từng phần tử khỏi mảng JavaScript.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop18
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 077
Original array: pop, splice, filter, shift Array Length: 081
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop7
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop9
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop5
Original array: pop, splice, filter, shift Array Length: 035
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____539
Removed element: shift Remaining elements: splice, filter, pop34
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop37
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop39
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080______742
Removed element: shift Remaining elements: splice, filter, pop43
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 07
Loại bỏ các phần tử Array bằng cách sử dụng phương thức filter[]. Phương thức này được sử dụng để tạo một mảng mới từ một mảng đã cho chỉ bao gồm các phần tử từ mảng đã cho thỏa mãn một điều kiện được đặt bởi hàm đối số. Để xóa các phần tử bằng phương thức filter[], bạn có thể chỉ định các phần tử theo nhiều cách khác nhau.
Thí dụ. Sử dụng giá trị của phương thức lọc để xóa các phần tử khỏi mảng JavaScript.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop47
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop0
Removed element: shift Remaining elements: splice, filter, pop50
________ 72 ________ 752 ________ 753
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop0
Removed element: shift Remaining elements: splice, filter, pop1
Removed element: shift Remaining elements: splice, filter, pop2______73
Removed element: shift Remaining elements: splice, filter, pop62
Removed element: shift Remaining elements: splice, filter, pop2____180______765
Removed element: shift Remaining elements: splice, filter, pop66
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 070
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 03
Loại bỏ các phần tử mảng bằng cách sử dụng Phương thức loại bỏ. Tạo phương thức xóa bằng phương thức bộ lọc để xóa các phần tử khỏi mảng JavaScript. Phương pháp này hoạt động theo thứ tự ngược lại.
Thí dụ.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 074
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 077
Removed element: shift Remaining elements: splice, filter, pop78
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop80
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop82
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop84
Original array: pop, splice, filter, shift Array Length: 035
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop88
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop0
Removed element: shift Remaining elements: splice, filter, pop91
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop93
Removed element: shift Remaining elements: splice, filter, pop52
Removed element: shift Remaining elements: splice, filter, pop95
Removed element: shift Remaining elements: splice, filter, pop0
Removed element: shift Remaining elements: splice, filter, pop97
Removed element: shift Remaining elements: splice, filter, pop98
Removed element: shift Remaining elements: splice, filter, pop52
Original array: pop, splice, filter, shift Array Length: 0800
Removed element: shift Remaining elements: splice, filter, pop93____1802
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 0809
Removed element: shift Remaining elements: splice, filter, pop82
Original array: pop, splice, filter, shift Array Length: 0811
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____185
Original array: pop, splice, filter, shift Array Length: 0815
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 08
Xóa phần tử mảng bằng toán tử Delete. Sử dụng toán tử xóa để xóa các phần tử khỏi mảng JavaScript.
Thí dụ.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 074
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 077
Removed element: shift Remaining elements: splice, filter, pop78
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop80
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop82
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop84
Original array: pop, splice, filter, shift Array Length: 035
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0833
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 0836
Original array: pop, splice, filter, shift Array Length: 0837
Original array: pop, splice, filter, shift Array Length: 0838
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____1842
Original array: pop, splice, filter, shift Array Length: 0843
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____185
Original array: pop, splice, filter, shift Array Length: 0847
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 09
Loại bỏ các phần tử Array bằng toán tử Clear and Reset. Sử dụng toán tử xóa và đặt lại để xóa các phần tử khỏi mảng JavaScript
ví dụ 1.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 074
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 077
Removed element: shift Remaining elements: splice, filter, pop78
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop80
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop82
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop84
Original array: pop, splice, filter, shift Array Length: 035
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0865
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 0868
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0871
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0873
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____742
Removed element: shift Remaining elements: splice, filter, pop34
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____539
Original array: pop, splice, filter, shift Array Length: 0881
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Removed element: shift Remaining elements: splice, filter, pop2
ví dụ 2.
jav
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 074
Original array: pop, splice, filter, shift Array Length: 04
Removed element: shift Remaining elements: splice, filter, pop3
Original array: pop, splice, filter, shift Array Length: 077
Removed element: shift Remaining elements: splice, filter, pop78
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop80
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop82
Removed element: shift Remaining elements: splice, filter, pop6
Removed element: shift Remaining elements: splice, filter, pop84
Original array: pop, splice, filter, shift Array Length: 035
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____539
Original array: pop, splice, filter, shift Array Length: 080
Original array: pop, splice, filter, shift Array Length: 081
Original array: pop, splice, filter, shift Array Length: 082
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0806
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0808
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080______742
Removed element: shift Remaining elements: splice, filter, pop43
Original array: pop, splice, filter, shift Array Length: 071
đầu ra.
Original array: pop, splice, filter, shift Array Length: 00
Xóa các phần tử Array bằng vòng lặp for[] đơn giản và một mảng mới. Ở đây, một for[] đơn giản sẽ được chạy trên mảng và ngoại trừ phần tử bị loại bỏ, các phần tử còn lại sẽ được đẩy vào mảng mới sẽ được khai báo bên trong hàm hoặc phương thức
Thí dụ
Javascript
Original array: pop, splice, filter, shift Array Length: 03
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0816
Original array: pop, splice, filter, shift Array Length: 0817
Original array: pop, splice, filter, shift Array Length: 0818
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0817
Original array: pop, splice, filter, shift Array Length: 087
Original array: pop, splice, filter, shift Array Length: 0822
Removed element: shift Remaining elements: splice, filter, pop2____092
Original array: pop, splice, filter, shift Array Length: 0825
Original array: pop, splice, filter, shift Array Length: 0826
Original array: pop, splice, filter, shift Array Length: 0827
Removed element: shift Remaining elements: splice, filter, pop2____188
Original array: pop, splice, filter, shift Array Length: 0817
Original array: pop, splice, filter, shift Array Length: 088
Original array: pop, splice, filter, shift Array Length: 0817
Removed element: shift Remaining elements: splice, filter, pop52
Original array: pop, splice, filter, shift Array Length: 0834
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0836
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0839
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0841
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0843
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 080____185
Original array: pop, splice, filter, shift Array Length: 0848
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 04
Original array: pop, splice, filter, shift Array Length: 0851
Original array: pop, splice, filter, shift Array Length: 071
đầu ra
Original array: pop, splice, filter, shift Array Length: 01
Xóa các phần tử Mảng bằng thư viện lowdash. Sử dụng thư viện lowdash để xóa các phần tử khỏi mảng JavaScript. Để sử dụng thư viện lowdash, bạn cần cài đặt nó cục bộ trên hệ thống của mình