Làm cách nào để kiểm tra xem một phần tử có trong javascript mảng 2d không?

Tìm kiếm tuyến tính là một thuật toán tìm kiếm đơn giản và tuần tự. Nó được sử dụng để tìm xem một phần tử cụ thể có trong mảng hay không bằng cách duyệt qua mọi phần tử trong mảng. Trong khi tìm kiếm trong mảng 2D hoàn toàn giống nhau nhưng ở đây cần phải duyệt qua tất cả các ô Theo cách này, bất kỳ phần tử nào cũng được tìm kiếm trong mảng 2D.  

Dưới đây là triển khai tìm kiếm tuyến tính trong mảng 2D

C++




Element found at index: [2, 3]
27

Element found at index: [2, 3]
28

Element found at index: [2, 3]
29
Element found at index: [2, 3]
30
Element found at index: [2, 3]
31

Element found at index: [2, 3]
32
Element found at index: [2, 3]
33
Element found at index: [2, 3]
0
Element found at index: [2, 3]
33
Element found at index: [2, 3]
2
Element found at index: [2, 3]
33
Element found at index: [2, 3]
4

Element found at index: [2, 3]
5

Element found at index: [2, 3]
6
Element found at index: [2, 3]
7
Element found at index: [2, 3]
8
Element found at index: [2, 3]
33
Element found at index: [2, 3]
0

Element found at index: [2, 3]
1
Element found at index: [2, 3]
7
Element found at index: [2, 3]
8
Element found at index: [2, 3]
33
Element found at index: [2, 3]
5

Element found at index: [2, 3]
6
Element found at index: [2, 3]
7
Element found at index: [2, 3]
8

Element found at index: [2, 3]
9
Element found at index: [2, 3]
270
Element found at index: [2, 3]
271

_______06____1273

_______01____1273

_______06____1273

Element found at index: [2, 3]
6
Element found at index: [2, 3]
270
Element found at index: [2, 3]
280

Element found at index: [2, 3]
273

 

Element found at index: [2, 3]
282

Element found at index: [2, 3]
33
Element found at index: [2, 3]
284

Element found at index: [2, 3]
5

 

Element found at index: [2, 3]
6
Element found at index: [2, 3]
287____133
Element found at index: [2, 3]
289

Element found at index: [2, 3]
290
Element found at index: [2, 3]
291

Element found at index: [2, 3]
290
Element found at index: [2, 3]
293

Element found at index: [2, 3]
6
Element found at index: [2, 3]
33
Element found at index: [2, 3]
296

_______06

Element found at index: [2, 3]
32____133
Element found at index: [2, 3]
300

Element found at index: [2, 3]
6
Element found at index: [2, 3]
302
Element found at index: [2, 3]
303
Element found at index: [2, 3]
304
Element found at index: [2, 3]
305
Element found at index: [2, 3]
306
Element found at index: [2, 3]
307
Element found at index: [2, 3]
308

 

Element found at index: [2, 3]
6
Element found at index: [2, 3]
270
Element found at index: [2, 3]
311

Element found at index: [2, 3]
273

 

Element found at index: [2, 3]
313

Java




Element found at index: [2, 3]
314

Element found at index: [2, 3]
315
Element found at index: [2, 3]
316

 

Element found at index: [2, 3]
317
Element found at index: [2, 3]
318
Element found at index: [2, 3]
319

Element found at index: [2, 3]
1
Element found at index: [2, 3]
317
Element found at index: [2, 3]
322
Element found at index: [2, 3]
323
Element found at index: [2, 3]
324

Element found at index: [2, 3]
1
Element found at index: [2, 3]
5

Element found at index: [2, 3]
9
Element found at index: [2, 3]
33
Element found at index: [2, 3]
329____1330
Element found at index: [2, 3]
331
Element found at index: [2, 3]
332
Element found at index: [2, 3]
331
Element found at index: [2, 3]
334
Element found at index: [2, 3]
335

Element found at index: [2, 3]
336
Element found at index: [2, 3]
5
Element found at index: [2, 3]
338
Element found at index: [2, 3]
331
Element found at index: [2, 3]
00
Element found at index: [2, 3]
331
Element found at index: [2, 3]
02
Element found at index: [2, 3]
335

Element found at index: [2, 3]
336
Element found at index: [2, 3]
5
Element found at index: [2, 3]
06
Element found at index: [2, 3]
331
Element found at index: [2, 3]
08
Element found at index: [2, 3]
331
Element found at index: [2, 3]
10
Element found at index: [2, 3]
11

Element found at index: [2, 3]
9
Element found at index: [2, 3]
33
Element found at index: [2, 3]
14
Element found at index: [2, 3]
02
Element found at index: [2, 3]
308

Element found at index: [2, 3]
9
Element found at index: [2, 3]
33
Element found at index: [2, 3]
19

Element found at index: [2, 3]
9
Element found at index: [2, 3]
21____022

Element found at index: [2, 3]
23
Element found at index: [2, 3]
24

_______01____1273

 

Element found at index: [2, 3]
1
Element found at index: [2, 3]
322
Element found at index: [2, 3]
33____030
Element found at index: [2, 3]
33
Element found at index: [2, 3]
32____133
Element found at index: [2, 3]
4

Element found at index: [2, 3]
1
Element found at index: [2, 3]
5

Element found at index: [2, 3]
9
Element found at index: [2, 3]
7
Element found at index: [2, 3]
8
Element found at index: [2, 3]
33
Element found at index: [2, 3]
41
Element found at index: [2, 3]
42
Element found at index: [2, 3]
43

Element found at index: [2, 3]
44
Element found at index: [2, 3]
7
Element found at index: [2, 3]
8
Element found at index: [2, 3]
33
Element found at index: [2, 3]
48
Element found at index: [2, 3]
42
Element found at index: [2, 3]
50

Element found at index: [2, 3]
51
Element found at index: [2, 3]
7
Element found at index: [2, 3]
8

Element found at index: [2, 3]
54
Element found at index: [2, 3]
270
Element found at index: [2, 3]
56
Element found at index: [2, 3]
33
Element found at index: [2, 3]
58

_______051____1273

_______044____1273

_______09____1273

Element found at index: [2, 3]
9
Element found at index: [2, 3]
270
Element found at index: [2, 3]
56
Element found at index: [2, 3]
33
Element found at index: [2, 3]
69
Element found at index: [2, 3]
70
Element found at index: [2, 3]
71
Element found at index: [2, 3]
70
Element found at index: [2, 3]
73

_______01____1273

Element found at index: [2, 3]
273

Python3




Element found at index: [2, 3]
77

Element found at index: [2, 3]
78
Element found at index: [2, 3]
79

Element found at index: [2, 3]
1
Element found at index: [2, 3]
7
Element found at index: [2, 3]
82
Element found at index: [2, 3]
83
Element found at index: [2, 3]
84
Element found at index: [2, 3]
8
Element found at index: [2, 3]
86
Element found at index: [2, 3]
87

Element found at index: [2, 3]
9
Element found at index: [2, 3]
7
Element found at index: [2, 3]
90
Element found at index: [2, 3]
83
Element found at index: [2, 3]
84
Element found at index: [2, 3]
8
Element found at index: [2, 3]
86
Element found at index: [2, 3]
95

Element found at index: [2, 3]
44
Element found at index: [2, 3]
7
Element found at index: [2, 3]
98
Element found at index: [2, 3]
99
Element found at index: [2, 3]
99
Element found at index: [2, 3]
01

Element found at index: [2, 3]
51
Element found at index: [2, 3]
270
Element found at index: [2, 3]
04

Element found at index: [2, 3]
1
Element found at index: [2, 3]
270
Element found at index: [2, 3]
07
Element found at index: [2, 3]
08
Element found at index: [2, 3]
70
Element found at index: [2, 3]
331
Element found at index: [2, 3]
08
Element found at index: [2, 3]
70
Element found at index: [2, 3]
13

 

Element found at index: [2, 3]
14

Element found at index: [2, 3]
15
Element found at index: [2, 3]
99
Element found at index: [2, 3]
17
Element found at index: [2, 3]
70
Element found at index: [2, 3]
331
Element found at index: [2, 3]
00
Element found at index: [2, 3]
331
Element found at index: [2, 3]
330
Element found at index: [2, 3]
331
Element found at index: [2, 3]
2965
Element found at index: [2, 3]
23
Element found at index: [2, 3]
338
Element found at index: [2, 3]
331
Element found at index: [2, 3]
2971
Element found at index: [2, 3]
331
Element found at index: [2, 3]
2973
Element found at index: [2, 3]
331
Element found at index: [2, 3]
2975
Element found at index: [2, 3]
23
Element found at index: [2, 3]
334
Element found at index: [2, 3]
331
Element found at index: [2, 3]
2981
Element found at index: [2, 3]
331
Element found at index: [2, 3]
2983
Element found at index: [2, 3]
331
Element found at index: [2, 3]
332
Element found at index: [2, 3]
35

Làm cách nào để kiểm tra xem một phần tử có trong mảng 2D trong JavaScript không?

Phương thức indexof[] trong Javascript là một trong những cách thuận tiện nhất để tìm hiểu xem một giá trị có tồn tại trong một mảng hay không. Phương thức indexof[] hoạt động trên hiện tượng số chỉ mục. Phương thức này trả về chỉ mục của mảng nếu tìm thấy và trả về -1 nếu không.

Làm cách nào để kiểm tra xem giá trị có tồn tại trong mảng 2D không?

Cách kiểm tra xem giá trị có tồn tại trong mảng 2D hay không .
biến x = 2;
biến y = 3;
var allObjects = new GameObject[x,y];
for [var a = 0; a < x; a++] {
for [var b = 0; b < y; b++] {
// Gán đối tượng cho mảng 2D
allObjects[a,b] =. ;

Làm cách nào để kiểm tra giá trị tồn tại trong JavaScript mảng đa chiều?

Sử dụng Phương thức indexOf[] . Nếu phần tử không tồn tại thì -1 được trả về.

Làm cách nào để kiểm tra xem một giá trị có tồn tại trong một mảng đối tượng trong JavaScript không?

Sử dụng phương thức include[] . Nếu mảng chứa một đối tượng/phần tử có thể được xác định bằng cách sử dụng phương thức include[]. Phương thức này trả về true nếu mảng chứa đối tượng/phần tử khác trả về false.

Chủ Đề