Hướng dẫn search javascript - tìm kiếm javascript

Trong Javascript có nhiều cách để tìm kiếm phần tử trong Array. Cách đơn giản nhất là dùng vòng lặp. Nhưng với ES6+ có nhiều methods để lặp trong Arrray và tìm kiếm trong các phần từ dễ dàng hơn.

Show

Dưới đây sẽ là những methods Array có thể sử dụng:

  • Array.includes
  • Array.find
  • Array.indexOf
  • Array.filter

includes

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.includes("thick scales"); // returns true

Method .includes() trả về giá trị

arr.includes(valueToFind, [fromIndex]);
0 là cách hoàn hảo dùng để kiểm tra phần tử có tồn tại trong array hay không, gía trị trả về của hàm này sẽ là
arr.includes(valueToFind, [fromIndex]);
1 hoặc
arr.includes(valueToFind, [fromIndex]);
2. Cú pháp đơn giản như sau

arr.includes(valueToFind, [fromIndex]);

Bây giờ như bạn thấy trong ví dụ trên, ta chỉ có một tham số - valueToFind ở dạng

arr.includes(valueToFind, [fromIndex]);
3. Tùy chọn từ
arr.includes(valueToFind, [fromIndex]);
4 là một số, cho biết chỉ số nào bạn muốn bắt đầu tìm kiếm (mặc định là 0, nên sẽ tìm kiếm toàn bộ phần tử trong mảng). Vì vậy, trong ví dụ trên,item
arr.includes(valueToFind, [fromIndex]);
5 có chỉ số 0, nên sau đây sẽ là
arr.includes(valueToFind, [fromIndex]);
2:
arr.includes(valueToFind, [fromIndex]);
7 kể từ khi nó bắt đầu tìm kiếm từ chỉ số 1 trở đi.

Có một vài điều quan trọng cần chú ý. Methdod .includes() này sử dụng so sánh rất nghiêm ngặt. Điều đó có nghĩa là, từ ví dụ trên, sau đây sẽ trả về

arr.includes(valueToFind, [fromIndex]);
2:
Uncaught TypeError: thick scales is not a function
0 Điều đó bởi vì mặc dù 80 == '80' là
arr.includes(valueToFind, [fromIndex]);
1, 80 === '80' là
arr.includes(valueToFind, [fromIndex]);
2

find

Method .find () khác với .include () như thế nào? Nếu trong ví dụ trên, ta chỉ thay đổi văn bản, trong đó có văn bản, trong đó có thể tìm thấy lỗi, ta sẽ gặp lỗi này:

Uncaught TypeError: thick scales is not a function

Bởi vì phương thức

Uncaught TypeError: thick scales is not a function
3 yêu cầu một
Uncaught TypeError: thick scales is not a function
4 được truyền vào. Vì phương thức
Uncaught TypeError: thick scales is not a function
3 không phải là sử dụng toán tử so sánh đơn giản như
Uncaught TypeError: thick scales is not a function
6. Thay vào đó, nó sẽ chuyển từng phần tử vào hàm của bạn và kiểm tra rồi trả về giá trị
arr.includes(valueToFind, [fromIndex]);
1 hay
Uncaught TypeError: thick scales is not a function
8. Vì vậy, mặc dù điều này hoạt động:
Uncaught TypeError: thick scales is not a function
9, bạn có thể muốn đưa toán tử so sánh của riêng mình vào hàm để nó trả về bất cứ điều gì có liên quan.

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'

Hàm đơn giản này trong method

Uncaught TypeError: thick scales is not a function
3 chúng ta xem xét từng phần tử của mảng, với
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
1 của
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
2, ta đã gán nó và dừng lại khi nó tìm thấy phần tử đầu tiên là đúng. Trong trường hợp
arr.includes(valueToFind, [fromIndex]);
1 có thuộc tính độ dài nhỏ hơn 12 (
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
4 không có thuộc tính độ dài). Tất nhiên bạn có thể làm cho chức năng này phức tạp khi cần thiết, làm cho điều kiện thực sự của bạn đáp ứng nhu cầu của bạn.

Trong ví dụ trên, ta chỉ sử dụng cuộc gọi lại với một tham số. Bạn cũng có thể thêm các tham số để tham chiếu

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
5 phần tử hiện tại. Một tham số khác có thể là
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
6, nhưng tôi thấy điều này hiếm khi được sử dụng. Dưới đây là một ví dụ sử dụng
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
5:

alligator.find((el, idx) => typeof el === "string" && idx === 2); // returns '4 foot tall'

Có 3

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
8 khác nhau đáp ứng điều kiện đầu tiên
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
9. Nếu đây là điều kiện duy nhất, nó sẽ trả lại cái đầu tiên
alligator.find((el, idx) => typeof el === "string" && idx === 2); // returns '4 foot tall'
0. Nhưng sự khác biệt là, chỉ có một người có chỉ số là 2 và đó là
alligator.find((el, idx) => typeof el === "string" && idx === 2); // returns '4 foot tall'
1

Nói về

alligator.find((el, idx) => typeof el === "string" && idx === 2); // returns '4 foot tall'
2, một phương thức mảng tương tự là .findIndex (). Phương thức này cũng nhận được một hàm, nhưng như bạn có thể đoán được, nó trả về chỉ số phần tử phù hợp thay vì chính phần tử đó.

indexOf

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3

Giống như phương thức .includes(),

alligator.find((el, idx) => typeof el === "string" && idx === 2); // returns '4 foot tall'
4 sử dụng so sánh nghiêm ngặt, không phải là hàm như chúng ta đã thấy với phương thức
alligator.find((el, idx) => typeof el === "string" && idx === 2); // returns '4 foot tall'
5. Nhưng không giống với
alligator.find((el, idx) => typeof el === "string" && idx === 2); // returns '4 foot tall'
6, nó trả về
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
5 của phần tử, thay vì
arr.includes(valueToFind, [fromIndex]);
0. Bạn cũng có thể chỉ ra
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
5 nào trong mảng để bắt đầu tìm kiếm.

Mình thấy

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
0 rất hữu ích. Nó nhanh chóng và dễ dàng, có thể cho bạn biết phần tử nằm trong mảng và có thể cho bạn biết phần tử có tồn tại hay không. Làm thế nào để nó cho bạn biết nếu các yếu tố tồn tại? Về cơ bản, chúng ta có thể biết phần tử tồn tại nếu nó trả về một số dương và nếu nó trả về
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
1thì chúng ta biết phần tử đó không tồn tại.

alligator.indexOf("soft and fluffy"); // returns -1
alligator.indexOf(80); // returns 1
alligator.indexOf(80, 2); // returns -1

Và như bạn có thể thấy, mặc dù chúng ta có thể lấy các phương thức

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
2 hoặc
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
3 để cung cấp cho chúng ta cùng một thông tin, nhưng điều này ít hơn rất nhiều để viết. Chúng tôi không phải viết ra một hàm để so sánh, vì nó đã nằm trong phương thức
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
4.

Bây giờ, giống như những cái khác,

alligator.find((el, idx) => typeof el === "string" && idx === 2); // returns '4 foot tall'
4 cũng trả về
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
5 của phần tử khớp đầu tiên mà nó tìm thấy.
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
7 cung cấp cho chúng ta một phương thức thay thế
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
8. Như bạn có thể đoán được là điều này thực hiện tương tự như
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
0 nhưng bắt đầu từ
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
5 cuối cùng của mảng và hoạt động ngược. Bạn cũng có thể chỉ định một tham số thứ hai, nhưng hãy nhớ các chỉ số không thay đổi, chỉ vì bạn đang sử dụng một phương thức khác.

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout", 80];

alligator.indexOf(80); // returns 1
alligator.lastIndexOf(80); // returns 4
alligator.indexOf(80, 2); // returns 4
alligator.lastIndexOf(80, 4); // returns 4
alligator.lastIndexOf(80, 3); // returns 1

Bonus: filter

const alligator = ["thick scales", 80, "4 foot tail", "rounded snout", 80];

alligator.filter(el => el === 80); //returns [80, 80]

Phương thức

alligator.indexOf("soft and fluffy"); // returns -1
alligator.indexOf(80); // returns 1
alligator.indexOf(80, 2); // returns -1
1 giống như phương thức
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
2, trong đó nó yêu cầu một
Uncaught TypeError: thick scales is not a function
4 được truyền và một điều kiện cho những gì sẽ được trả về. Sự khác biệt chính là
alligator.indexOf("soft and fluffy"); // returns -1
alligator.indexOf(80); // returns 1
alligator.indexOf(80, 2); // returns -1
1 luôn trả về một
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.find(el => el.length < 12); // returns '4 foot tail'
6, ngay cả khi chỉ có một phần tử phù hợp. Nhưng nó sẽ trả về tất cả các phần tử khớp, trong khi
const alligator = ["thick scales", 80, "4 foot tail", "rounded snout"];

alligator.indexOf("rounded snout"); // returns 3
2 chỉ trả về kết quả đầu tiên.