Làm thế nào để bạn bật một yếu tố cụ thể?

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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
5

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
7

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
0
Removed element: shift
Remaining elements: splice, filter, pop
1

Removed element: shift
Remaining elements: splice, filter, pop
2______73
Removed element: shift
Remaining elements: splice, filter, pop
4
Removed element: shift
Remaining elements: splice, filter, pop
5
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
7
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
9
Removed element: shift
Remaining elements: splice, filter, pop
6
Original array: pop, splice, filter, shift
Array Length: 0
81
Original array: pop, splice, filter, shift
Array Length: 0
82

Original array: pop, splice, filter, shift
Array Length: 0
4

Removed element: shift
Remaining elements: splice, filter, pop
2____185

Removed element: shift
Remaining elements: splice, filter, pop
2____73
Original array: pop, splice, filter, shift
Array Length: 0
88

Removed element: shift
Remaining elements: splice, filter, pop
2____180____181
Original array: pop, splice, filter, shift
Array Length: 0
82

Removed element: shift
Remaining elements: splice, filter, pop
2____180____185
Original array: pop, splice, filter, shift
Array Length: 0
86

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
70

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
9

ví dụ 2.  

jav




Original array: pop, splice, filter, shift
Array Length: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
74

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
77
Original array: pop, splice, filter, shift
Array Length: 0
81
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
7
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
9
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
5
Original array: pop, splice, filter, shift
Array Length: 0
35

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____539
Original array: pop, splice, filter, shift
Array Length: 0
80
Original array: pop, splice, filter, shift
Array Length: 0
81
Original array: pop, splice, filter, shift
Array Length: 0
82

Original array: pop, splice, filter, shift
Array Length: 0
4

_______04____585

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
87
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4

Removed element: shift
Remaining elements: splice, filter, pop
2
Original array: pop, splice, filter, shift
Array Length: 0
91

Removed element: shift
Remaining elements: splice, filter, pop
2
Original array: pop, splice, filter, shift
Array Length: 0
93

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____698
Original array: pop, splice, filter, shift
Array Length: 0
99

Original array: pop, splice, filter, shift
Array Length: 0
71

đầ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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
23

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
25

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
0
Removed element: shift
Remaining elements: splice, filter, pop
1

Removed element: shift
Remaining elements: splice, filter, pop
2______73
Removed element: shift
Remaining elements: splice, filter, pop
4
Removed element: shift
Remaining elements: splice, filter, pop
5
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
7
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
9
Removed element: shift
Remaining elements: splice, filter, pop
6
Original array: pop, splice, filter, shift
Array Length: 0
81
Original array: pop, splice, filter, shift
Array Length: 0
82

Original array: pop, splice, filter, shift
Array Length: 0
4

Removed element: shift
Remaining elements: splice, filter, pop
2____012

Removed element: shift
Remaining elements: splice, filter, pop
2
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
15

Removed element: shift
Remaining elements: splice, filter, pop
2____180____181
Original array: pop, splice, filter, shift
Array Length: 0
19

Removed element: shift
Remaining elements: splice, filter, pop
2____180____185
Original array: pop, splice, filter, shift
Array Length: 0
86

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
70

Original array: pop, splice, filter, shift
Array Length: 0
71

đầ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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
31

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
0
Removed element: shift
Remaining elements: splice, filter, pop
1

Removed element: shift
Remaining elements: splice, filter, pop
2______73
Removed element: shift
Remaining elements: splice, filter, pop
4
Removed element: shift
Remaining elements: splice, filter, pop
5
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
7
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
9
Removed element: shift
Remaining elements: splice, filter, pop
6
Original array: pop, splice, filter, shift
Array Length: 0
81
Original array: pop, splice, filter, shift
Array Length: 0
82

Original array: pop, splice, filter, shift
Array Length: 0
4

_______72____049

Removed element: shift
Remaining elements: splice, filter, pop
2____73
Original array: pop, splice, filter, shift
Array Length: 0
52

Removed element: shift
Remaining elements: splice, filter, pop
2____180____181
Original array: pop, splice, filter, shift
Array Length: 0
56

Removed element: shift
Remaining elements: splice, filter, pop
2____180____185
Original array: pop, splice, filter, shift
Array Length: 0
86

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
70

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
8

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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
31

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
0
Removed element: shift
Remaining elements: splice, filter, pop
1

Removed element: shift
Remaining elements: splice, filter, pop
2______73
Removed element: shift
Remaining elements: splice, filter, pop
4
Removed element: shift
Remaining elements: splice, filter, pop
5
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
7
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
9
Removed element: shift
Remaining elements: splice, filter, pop
6
Original array: pop, splice, filter, shift
Array Length: 0
81
Original array: pop, splice, filter, shift
Array Length: 0
82

Original array: pop, splice, filter, shift
Array Length: 0
4

Removed element: shift
Remaining elements: splice, filter, pop
2____085

Removed element: shift
Remaining elements: splice, filter, pop
2
Original array: pop, splice, filter, shift
Array Length: 0
87
Original array: pop, splice, filter, shift
Array Length: 0
88
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
90

Original array: pop, splice, filter, shift
Array Length: 0
91
Original array: pop, splice, filter, shift
Array Length: 0
92
Original array: pop, splice, filter, shift
Array Length: 0
93
Removed element: shift
Remaining elements: splice, filter, pop
7
Original array: pop, splice, filter, shift
Array Length: 0
95

Original array: pop, splice, filter, shift
Array Length: 0
96
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
98

Original array: pop, splice, filter, shift
Array Length: 0
96
Original array: pop, splice, filter, shift
Array Length: 0
80
Original array: pop, splice, filter, shift
Array Length: 0
81
Original array: pop, splice, filter, shift
Array Length: 0
56

Original array: pop, splice, filter, shift
Array Length: 0
96
Original array: pop, splice, filter, shift
Array Length: 0
80
Original array: pop, splice, filter, shift
Array Length: 0
85
Original array: pop, splice, filter, shift
Array Length: 0
86

_______091____188

Removed element: shift
Remaining elements: splice, filter, pop
2____188

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
70

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
8

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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
18

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
77
Original array: pop, splice, filter, shift
Array Length: 0
81
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
7
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
9
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
5
Original array: pop, splice, filter, shift
Array Length: 0
35

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____539
Removed element: shift
Remaining elements: splice, filter, pop
34

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
37

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
39

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80______742
Removed element: shift
Remaining elements: splice, filter, pop
43

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
7

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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
47

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
0
Removed element: shift
Remaining elements: splice, filter, pop
50

________ 72 ________ 752 ________ 753

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
0
Removed element: shift
Remaining elements: splice, filter, pop
1

Removed element: shift
Remaining elements: splice, filter, pop
2______73
Removed element: shift
Remaining elements: splice, filter, pop
62

Removed element: shift
Remaining elements: splice, filter, pop
2____180______765
Removed element: shift
Remaining elements: splice, filter, pop
66

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
70

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
3

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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
74

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
77
Removed element: shift
Remaining elements: splice, filter, pop
78
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
80
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
82
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
84
Original array: pop, splice, filter, shift
Array Length: 0
35

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
88

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
0
Removed element: shift
Remaining elements: splice, filter, pop
91

Original array: pop, splice, filter, shift
Array Length: 0
4

Removed element: shift
Remaining elements: splice, filter, pop
93
Removed element: shift
Remaining elements: splice, filter, pop
52
Removed element: shift
Remaining elements: splice, filter, pop
95
Removed element: shift
Remaining elements: splice, filter, pop
0
Removed element: shift
Remaining elements: splice, filter, pop
97

Removed element: shift
Remaining elements: splice, filter, pop
98
Removed element: shift
Remaining elements: splice, filter, pop
52
Original array: pop, splice, filter, shift
Array Length: 0
800

Removed element: shift
Remaining elements: splice, filter, pop
93____1802

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
809
Removed element: shift
Remaining elements: splice, filter, pop
82
Original array: pop, splice, filter, shift
Array Length: 0
811

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____185
Original array: pop, splice, filter, shift
Array Length: 0
815

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
8

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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
74

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
77
Removed element: shift
Remaining elements: splice, filter, pop
78
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
80
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
82
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
84
Original array: pop, splice, filter, shift
Array Length: 0
35

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
833

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
836
Original array: pop, splice, filter, shift
Array Length: 0
837
Original array: pop, splice, filter, shift
Array Length: 0
838

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____1842
Original array: pop, splice, filter, shift
Array Length: 0
843

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____185
Original array: pop, splice, filter, shift
Array Length: 0
847

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
9

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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
74

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
77
Removed element: shift
Remaining elements: splice, filter, pop
78
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
80
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
82
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
84
Original array: pop, splice, filter, shift
Array Length: 0
35

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
865

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
868

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
871

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
873

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____742
Removed element: shift
Remaining elements: splice, filter, pop
34

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____539
Original array: pop, splice, filter, shift
Array Length: 0
881

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Removed element: shift
Remaining elements: splice, filter, pop
2

ví dụ 2.  

jav




Original array: pop, splice, filter, shift
Array Length: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
74

Original array: pop, splice, filter, shift
Array Length: 0
4
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
77
Removed element: shift
Remaining elements: splice, filter, pop
78
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
80
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
82
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
84
Original array: pop, splice, filter, shift
Array Length: 0
35

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____539
Original array: pop, splice, filter, shift
Array Length: 0
80
Original array: pop, splice, filter, shift
Array Length: 0
81
Original array: pop, splice, filter, shift
Array Length: 0
82

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
806

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
808

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80______742
Removed element: shift
Remaining elements: splice, filter, pop
43

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
0

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: 0
3

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
816

Original array: pop, splice, filter, shift
Array Length: 0
817
Original array: pop, splice, filter, shift
Array Length: 0
818

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
817
Original array: pop, splice, filter, shift
Array Length: 0
87
Original array: pop, splice, filter, shift
Array Length: 0
822

Removed element: shift
Remaining elements: splice, filter, pop
2____092
Original array: pop, splice, filter, shift
Array Length: 0
825

Original array: pop, splice, filter, shift
Array Length: 0
826
Original array: pop, splice, filter, shift
Array Length: 0
827

Removed element: shift
Remaining elements: splice, filter, pop
2____188

Original array: pop, splice, filter, shift
Array Length: 0
817
Original array: pop, splice, filter, shift
Array Length: 0
88

Original array: pop, splice, filter, shift
Array Length: 0
817
Removed element: shift
Remaining elements: splice, filter, pop
52
Original array: pop, splice, filter, shift
Array Length: 0
834

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
836

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
839

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
841

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
843

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
80____185
Original array: pop, splice, filter, shift
Array Length: 0
848

Original array: pop, splice, filter, shift
Array Length: 0
4

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
851

Original array: pop, splice, filter, shift
Array Length: 0
71

đầu ra

Original array: pop, splice, filter, shift
Array Length: 0
1

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

Pop có xóa phần tử đầu tiên hoặc cuối cùng không?

Phương thức pop[] xóa phần tử cuối cùng khỏi một mảng và trả về giá trị đó cho người gọi. Nếu bạn gọi pop[] trên một mảng trống, nó sẽ trả về không xác định.

Làm cách nào để xóa phần tử cụ thể khỏi đối tượng trong JavaScript?

Xóa Thuộc tính khỏi Đối tượng . Sau khi xóa, tài sản không thể được sử dụng trước khi nó được thêm lại. Toán tử xóa được thiết kế để sử dụng trên các thuộc tính đối tượng. Nó không ảnh hưởng đến các biến hoặc chức năng. The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

Chủ Đề