# jQuery.inArray()

Mô tả: Tìm kiếm một giá trị được chỉ định trong một mảng và trả về chỉ mục của nó (hoặc -1 nếu không tìm thấy).

```
value
Type: Anything
The value to search for.
array
Type: Array
An array through which to search.
fromIndex
Type: Number
Chỉ mục của mảng bắt đầu tìm kiếm. Mặc định là 0, sẽ tìm kiếm toàn bộ mảng.
```

Phương thức $ .inArray () tương tự như phương thức .indexOf () gốc của JavaScript ở chỗ nó trả về -1 khi không tìm thấy kết quả khớp. Nếu phần tử đầu tiên trong mảng khớp với giá trị, $ .inArray () trả về 0.

Vì JavaScript coi 0 là lỏng lẻo bằng false (nghĩa là 0 == false, nhưng 0! == false), để kiểm tra sự hiện diện của giá trị trong mảng, bạn cần kiểm tra xem nó có bằng (hoặc lớn hơn) -1 không .

Việc so sánh giữa các giá trị là nghiêm ngặt. Sau đây sẽ trả về -1 (không tìm thấy) vì một số đang được tìm kiếm trong một chuỗi các chuỗi:

```
$.inArray( 5 + 5, [ "8", "9", "10", 10 + "" ] );
```

```
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.inArray demo</title>
  <style>
    div {
      color: blue;
    }
    span {
      color: red;
    }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
  <div>"John" found at <span></span></div>
  <div>4 found at <span></span></div>
  <div>"Karl" not found, so <span></span></div>
  <div>"Pete" is in the array, but not at or after index 2, so <span></span></div>
  <script>
    var arr = [4, "Pete", 8, "John"];
    var $spans = $("span");
    $spans.eq(0).text(jQuery.inArray("John", arr));
    $spans.eq(1).text(jQuery.inArray(4, arr));
    $spans.eq(2).text(jQuery.inArray("Karl", arr));
    $spans.eq(3).text(jQuery.inArray("Pete", arr, 2));
  </script>
</body>
</html>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/advanced/jquery.inarray.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
