Phương thức
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
8 trả về phần tử đầu tiên trong mảng được cung cấp đáp ứng chức năng kiểm tra được cung cấp. Nếu không có giá trị nào thỏa mãn chức năng kiểm tra, thì trả về // Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
9Thử nó
- Nếu bạn cần chỉ mục của phần tử được tìm thấy trong mảng, hãy sử dụng
0const inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
- Nếu bạn cần tìm chỉ mục của một giá trị, hãy sử dụng
1. [Tương tự nhưconst inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
0, nhưng kiểm tra từng phần tử xem có bằng với giá trị thay vì sử dụng hàm kiểm tra. ]const inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
- Nếu bạn cần tìm xem một giá trị có tồn tại trong một mảng hay không, hãy sử dụng
1. Một lần nữa, nó kiểm tra từng phần tử bằng với giá trị thay vì sử dụng hàm kiểm traconst inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
- Nếu bạn cần tìm xem có phần tử nào thỏa mãn chức năng kiểm tra được cung cấp hay không, hãy sử dụng
2const inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
cú pháp
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
Thông số
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
3Một hàm để thực thi cho từng phần tử trong mảng. Nó sẽ trả về một giá trị trung thực để cho biết một phần tử phù hợp đã được tìm thấy
Hàm được gọi với các đối số sau
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
4Phần tử hiện tại đang được xử lý trong mảng
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
5Chỉ số của phần tử hiện tại đang được xử lý trong mảng
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
6Mảng
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
8 đã được gọiconst inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
8 Tùy chọnMột giá trị được sử dụng như
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
9 khi thực thi const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
3. Xem các phương pháp lặpGiá trị trả về
Phần tử đầu tiên trong mảng thỏa mãn chức năng kiểm tra được cung cấp. Mặt khác,
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
9 được trả lạiSự miêu tả
Phương pháp
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
8 là một phương pháp lặp. Nó gọi một hàm const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
3 được cung cấp một lần cho mỗi phần tử trong một mảng theo thứ tự chỉ số tăng dần, cho đến khi const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
3 trả về một giá trị trung thực. // Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
8 sau đó trả về phần tử đó và ngừng lặp qua mảng. Nếu const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
3 không bao giờ trả về giá trị trung thực, thì // Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
8 trả về // Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
9const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
3 được gọi cho mọi chỉ mục của mảng, không chỉ những chỉ mục có giá trị được gán. Các vị trí trống trong các mảng thưa thớt hoạt động giống như // Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
9// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
8 không thay đổi mảng mà nó được gọi, nhưng hàm được cung cấp dưới dạng const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
3 có thể. Tuy nhiên, lưu ý rằng độ dài của mảng được lưu trước lần gọi đầu tiên của const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
3. Vì vậy
3 sẽ không truy cập bất kỳ phần tử nào được thêm vào ngoài độ dài ban đầu của mảng khi cuộc gọi đếnconst inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
8 bắt đầu// Arrow function find[[element] => { /* … */ }] find[[element, index] => { /* … */ }] find[[element, index, array] => { /* … */ }] // Callback function find[callbackFn] find[callbackFn, thisArg] // Inline callback function find[function [element] { /* … */ }] find[function [element, index] { /* … */ }] find[function [element, index, array] { /* … */ }] find[function [element, index, array] { /* … */ }, thisArg]
- Các thay đổi đối với các chỉ mục đã truy cập không khiến
3 được gọi lại trên chúngconst inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
- Nếu một phần tử hiện có, chưa được truy cập của mảng bị thay đổi bởi
3, thì giá trị của nó được truyền choconst inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
3 sẽ là giá trị tại thời điểm phần tử đó được truy cập. Các phần tử đã xóa được truy cập như thể chúng làconst inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, ]; function isCherries[fruit] { return fruit.name === "cherries"; } console.log[inventory.find[isCherries]]; // { name: 'cherries', quantity: 5 }
9// Arrow function find[[element] => { /* … */ }] find[[element, index] => { /* … */ }] find[[element, index, array] => { /* … */ }] // Callback function find[callbackFn] find[callbackFn, thisArg] // Inline callback function find[function [element] { /* … */ }] find[function [element, index] { /* … */ }] find[function [element, index, array] { /* … */ }] find[function [element, index, array] { /* … */ }, thisArg]
Cảnh báo. Các sửa đổi đồng thời thuộc loại được mô tả ở trên thường dẫn đến mã khó hiểu và thường nên tránh [trừ trường hợp đặc biệt]
Phương pháp
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
8 là chung chung. Nó chỉ mong đợi giá trị const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
9 có thuộc tính // Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
12 và các thuộc tính có khóa số nguyênví dụ
Tìm một đối tượng trong một mảng bằng một trong các thuộc tính của nó
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
Sử dụng chức năng mũi tên và phá hủy
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
3Tìm một số nguyên tố trong một mảng
Ví dụ sau tìm phần tử trong mảng là số nguyên tố [hoặc trả về
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
9 nếu không có số nguyên tố]// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
4Sử dụng find[] trên các mảng thưa thớt
Các vị trí trống trong các mảng thưa thớt được truy cập và được xử lý giống như
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
9// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
1Gọi find[] trên các đối tượng không phải mảng
Phương thức
// Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
8 đọc thuộc tính // Arrow function
find[[element] => { /* … */ }]
find[[element, index] => { /* … */ }]
find[[element, index, array] => { /* … */ }]
// Callback function
find[callbackFn]
find[callbackFn, thisArg]
// Inline callback function
find[function [element] { /* … */ }]
find[function [element, index] { /* … */ }]
find[function [element, index, array] { /* … */ }]
find[function [element, index, array] { /* … */ }, thisArg]
12 của const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries[fruit] {
return fruit.name === "cherries";
}
console.log[inventory.find[isCherries]];
// { name: 'cherries', quantity: 5 }
9 và sau đó truy cập từng chỉ mục số nguyên