Javascript xóa hàng khỏi mảng

Hộp thoại phụ INPUT ARRAY xử lý phiên bản danh sách bản ghi. Bộ điều khiển này cho phép người dùng chỉnh sửa trực tiếp các hàng hiện có và tạo hoặc xóa các hàng bằng các hành động ngầm định

Các hàng có thể được tạo bằng hành động nối thêm hoặc chèn và có thể được xóa bằng hành động xóa. Ba hành động ngầm định này được tạo tự động bởi hộp thoại

  • Hành động chèn tạo một hàng mới trước hàng hiện tại. Nếu không có hàng nào trong danh sách, hãy chèn thêm một hàng mới
  • Hành động chắp thêm tạo một hàng mới sau hàng cuối cùng của danh sách
  • Thao tác xóa sẽ xóa hàng hiện tại

Để ngăn INPUT ARRAY tạo ra các hành động ẩn này, hãy đặt các thuộc tính kiểm soát APPEND ROW, INSERT ROW và/hoặc DELETE ROW thành FALSE. Để từ chối hoàn toàn việc thêm hàng, cũng đặt thuộc tính AUTO APPEND thành FALSE.

DIALOG ATTRIBUTES(UNBUFFERED)
  INPUT ARRAY p_items FROM sa.*
    ATTRIBUTES(AUTO APPEND=FALSE,
               INSERT ROW=FALSE)
     ...
  END INPUT
END DIALOG

Các khối điều khiển cụ thể có sẵn để kiểm soát khi một hàng được tạo hoặc xóa

  • Các khối điều khiển TRƯỚC CHÈN và SAU KHI CHÈN có thể được sử dụng để điều khiển việc tạo hàng. Hủy tạo hàng bằng CANCEL INSERT trong khối TRƯỚC CHÈN hoặc SAU KHI CHÈN
  • Các khối điều khiển TRƯỚC KHI XÓA và SAU KHI XÓA có thể được sử dụng để điều khiển việc xóa hàng. Hủy xóa hàng bằng lệnh CANCEL DELETE trong TRƯỚC KHI XÓA

Mảng động và giao diện người dùng. Lớp hộp thoại cung cấp các phương thức như mảng. xóaElement() hoặc ui. hộp thoại. appendRow() để sửa đổi danh sách. Khi sử dụng các phương thức này, các trình kích hoạt được xác định trước như TRƯỚC KHI XÓA hoặc TRƯỚC KHI CHÈN không được thực thi. Mặc dù việc sử dụng các phương pháp này trong Mảng HIỂN THỊ là an toàn, nhưng bạn phải cẩn thận khi sử dụng Mảng ĐẦU VÀO. Ví dụ: bạn không nên gọi các phương thức như vậy trong trình kích hoạt như TRƯỚC HÀNG, SAU KHI CHÈN, TRƯỚC KHI XÓA

Người dùng có thể nối thêm các hàng tạm thời bằng cách di chuyển đến cuối danh sách hoặc khi thực hiện hành động nối thêm. Việc thêm các hàng tạm thời khác với việc thực hiện hành động chèn;

Theo mặc định, khi hàng cuối cùng bị xóa bởi thao tác xóa, lệnh INPUT ARRAY sẽ tự động tạo một hàng tạm thời mới ở cùng vị trí. Hiệu ứng hình ảnh của hành vi này có thể bị hiểu sai - nếu không có dữ liệu nào được nhập vào hàng cuối cùng, bạn sẽ không thấy bất kỳ sự khác biệt nào. Tuy nhiên, hàng cuối cùng thực sự bị xóa và một hàng mới được tạo, và chuỗi khối điều khiển TRƯỚC XÓA / SAU KHI XÓA / SAU ROW / TRƯỚC ROW / TRƯỚC CHÈN được thực thi. Để từ chối để tránh việc tạo một hàng tạm thời mới khi hàng cuối cùng bị xóa, hãy đặt thuộc tính AUTO APPEND = FALSE

Các hành động chèn, nối thêm hoặc xóa có thể tự động bị vô hiệu hóa tùy theo ngữ cảnh. Nếu INPUT ARRAY đang sử dụng một mảng tĩnh đã đầy hoặc nếu đã đạt đến thuộc tính MAXCOUNT, thì cả thao tác chèn và nối thêm sẽ bị vô hiệu hóa. Hành động xóa tự động bị tắt khi AUTO APPEND = FALSE và không còn hàng nào trong mảng

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ị. Bạn có thể thêm bớt các 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.  

Javascript xóa hàng 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 pop(). Phương thức này dùng để loại bỏ các phần tử ở cuối mảng
  • hàm shift(). 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
  • chức năng mối nối (). 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
  • chức năng bộ lọc(). 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
8

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
Removed element: shift
Remaining elements: splice, filter, pop
3
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
91
Original array: pop, splice, filter, shift
Array Length: 0
92

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

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

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
98

Removed element: shift
Remaining elements: splice, filter, pop
2
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
Original array: pop, splice, filter, shift
Array Length: 0
94

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

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

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

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

đầu ra.  

Original array: pop, splice, filter, shift
Array Length: 0
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
86

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
89
Original array: pop, splice, filter, shift
Array Length: 0
91
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
67

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

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
90
Original array: pop, splice, filter, shift
Array Length: 0
01
Original array: pop, splice, filter, shift
Array Length: 0
02
Original array: pop, splice, filter, shift
Array Length: 0
93
Original array: pop, splice, filter, shift
Array Length: 0
04

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

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

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

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

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

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

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

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
90
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

đầ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
Original array: pop, splice, filter, shift
Array Length: 0
95

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

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
Removed element: shift
Remaining elements: splice, filter, pop
3
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
91
Original array: pop, splice, filter, shift
Array Length: 0
92

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

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

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
17

Removed element: shift
Remaining elements: splice, filter, pop
2
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
21
Original array: pop, splice, filter, shift
Array Length: 0
93
Original array: pop, splice, filter, shift
Array Length: 0
94

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

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

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

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

đầu ra.  

________số 8

Loại bỏ các phần tử Array bằ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 mối nối(), 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 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
35

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

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
Removed element: shift
Remaining elements: splice, filter, pop
3
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
91
Original array: pop, splice, filter, shift
Array Length: 0
92

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

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

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
56

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

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

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

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

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

đầu ra.  

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

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
72

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

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

Removed element: shift
Remaining elements: splice, filter, pop
2
Removed element: shift
Remaining elements: splice, filter, pop
3
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
91
Original array: pop, splice, filter, shift
Array Length: 0
88

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

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
94
Removed element: shift
Remaining elements: splice, filter, pop
3
Original array: pop, splice, filter, shift
Array Length: 0
96

Original array: pop, splice, filter, shift
Array Length: 0
97
Original array: pop, splice, filter, shift
Array Length: 0
98
Original array: pop, splice, filter, shift
Array Length: 0
99
Removed element: shift
Remaining elements: splice, filter, pop
7
Removed element: shift
Remaining elements: splice, filter, pop
01

Removed element: shift
Remaining elements: splice, filter, pop
02
Removed element: shift
Remaining elements: splice, filter, pop
3
Removed element: shift
Remaining elements: splice, filter, pop
04

Removed element: shift
Remaining elements: splice, filter, pop
02
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
60
Original array: pop, splice, filter, shift
Array Length: 0
93
Removed element: shift
Remaining elements: splice, filter, pop
10

Removed element: shift
Remaining elements: splice, filter, pop
02
Original array: pop, splice, filter, shift
Array Length: 0
90____197
Removed element: shift
Remaining elements: splice, filter, pop
14

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

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

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

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

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

đầu ra.  

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

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
26

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
89
Original array: pop, splice, filter, shift
Array Length: 0
91
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
67

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

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
90
Original array: pop, splice, filter, shift
Array Length: 0
01
Original array: pop, splice, filter, shift
Array Length: 0
02
Original array: pop, splice, filter, shift
Array Length: 0
93
Original array: pop, splice, filter, shift
Array Length: 0
04

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

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
49

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

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

đầu ra.  

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

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
57

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
60

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

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

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

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
Removed element: shift
Remaining elements: splice, filter, pop
3
Removed element: shift
Remaining elements: splice, filter, pop
72

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

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

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

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

đầu ra.  

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

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 lọc để xóa 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
86

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
89
Removed element: shift
Remaining elements: splice, filter, pop
88
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
90
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
92
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
94
Original array: pop, splice, filter, shift
Array Length: 0
67

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

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

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

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

Original array: pop, splice, filter, shift
Array Length: 0
903
Removed element: shift
Remaining elements: splice, filter, pop
62
Original array: pop, splice, filter, shift
Array Length: 0
905
Removed element: shift
Remaining elements: splice, filter, pop
0
Original array: pop, splice, filter, shift
Array Length: 0
907

Original array: pop, splice, filter, shift
Array Length: 0
908
Removed element: shift
Remaining elements: splice, filter, pop
62
Original array: pop, splice, filter, shift
Array Length: 0
910

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

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

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

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

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
919
Removed element: shift
Remaining elements: splice, filter, pop
92
Removed element: shift
Remaining elements: splice, filter, pop
10

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

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

đầu ra.  

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

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
86

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
89
Removed element: shift
Remaining elements: splice, filter, pop
88
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
90
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
92
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
94
Original array: pop, splice, filter, shift
Array Length: 0
67

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

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

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
946
Original array: pop, splice, filter, shift
Array Length: 0
947
Original array: pop, splice, filter, shift
Array Length: 0
948

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

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
90
Original array: pop, splice, filter, shift
Array Length: 0
952
Original array: pop, splice, filter, shift
Array Length: 0
953
Original array: pop, splice, filter, shift
Array Length: 0
93
Removed element: shift
Remaining elements: splice, filter, pop
10

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

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

đầu ra.  

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

Loại bỏ các phần tử mảng bằng toán tử xóa và đặt lại. 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
86

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
89
Removed element: shift
Remaining elements: splice, filter, pop
88
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
90
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
92
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
94
Original array: pop, splice, filter, shift
Array Length: 0
67

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

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

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
980

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

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

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

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
90
Removed element: shift
Remaining elements: splice, filter, pop
52
Original array: pop, splice, filter, shift
Array Length: 0
02
Original array: pop, splice, filter, shift
Array Length: 0
93
Original array: pop, splice, filter, shift
Array Length: 0
04

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

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

đầ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
86

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
89
Removed element: shift
Remaining elements: splice, filter, pop
88
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
90
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
92
Removed element: shift
Remaining elements: splice, filter, pop
6
Removed element: shift
Remaining elements: splice, filter, pop
94
Original array: pop, splice, filter, shift
Array Length: 0
67

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

Original array: pop, splice, filter, shift
Array Length: 0
4
Original array: pop, splice, filter, shift
Array Length: 0
90
Original array: pop, splice, filter, shift
Array Length: 0
01
Original array: pop, splice, filter, shift
Array Length: 0
02
Original array: pop, splice, filter, shift
Array Length: 0
93
Original array: pop, splice, filter, shift
Array Length: 0
04

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

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

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

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

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

đầ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
72

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

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

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

Original array: pop, splice, filter, shift
Array Length: 0
931
Original array: pop, splice, filter, shift
Array Length: 0
93
Original array: pop, splice, filter, shift
Array Length: 0
936

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

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

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

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

Original array: pop, splice, filter, shift
Array Length: 0
931
Removed element: shift
Remaining elements: splice, filter, pop
62
Original array: pop, splice, filter, shift
Array Length: 0
948

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

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

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

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

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

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

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

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

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

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

đầ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

Mối nối có làm thay đổi mảng không?

Phương thức splice() là một phương thức đột biến . Nó có thể thay đổi nội dung của điều này. Nếu số lượng phần tử được chỉ định để chèn vào khác với số lượng phần tử bị xóa, thì độ dài của mảng cũng sẽ thay đổi.

Mối nối JavaScript hoạt động như thế nào?

Phương thức mối nối () là một phương thức tích hợp cho các đối tượng Mảng JavaScript. Nó cho phép bạn thay đổi nội dung của mảng bằng cách xóa hoặc thay thế các phần tử hiện có bằng phần tử mới. Phương thức này sửa đổi mảng ban đầu và trả về các phần tử đã xóa dưới dạng một mảng mới