Phương thức find[]
trả về phần tử đầu tiên trong mảng được cung cấp thỏa mãn chức năng thử nghiệm được cung cấp. Nếu không có giá trị thỏa mãn chức năng kiểm tra, undefined
sẽ được trả về.Thử nó
findIndex[]
.index of the found element in the array, use findIndex[]
.indexOf[]
. [Nó tương tự như findIndex[]
, nhưng kiểm tra từng phần tử cho sự bình đẳng với giá trị thay vì sử dụng hàm thử nghiệm.]index of a value, use
indexOf[]
. [It's similar to findIndex[]
, but checks each element for equality with the value instead of using a testing function.]
1. Một lần nữa, nó kiểm tra từng phần tử cho sự bình đẳng với giá trị thay vì sử dụng hàm thử nghiệm.exists in an array, use
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 }
1. Again, it checks each element for equality with the value instead of using a testing function. 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 }
2.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 }
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 }
3Chức năng để thực thi trên mỗi giá trị trong mảng.
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 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ố [vị trí] của phần tử hiện tại 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 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 }
7 đã được gọi.Cuộc gọi lại phải trả về một giá trị sự thật để chỉ ra một yếu tố phù hợp đã được tìm thấy.
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 }
8 Tùy chọnOptionalĐối tượng sử dụ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 bên trong 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.Giá trị trả về
Phần tử đầu tiên trong mảng thỏa mãn chức năng thử nghiệm được cung cấp. Nếu không, undefined
được trả lại.
Sự mô tả
Phương thức
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 }
7 thực thi 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 một lần cho mỗi chỉ mục của mảng 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ề giá trị sự thật. Nếu vậy, 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 }
7 ngay lập tức trả về giá trị của phần tử đó. Nếu khô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 }
7 trả về undefined
.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 gọi cho mọi chỉ mục của mảng, không chỉ các chỉ số có giá trị được gán. Các khe trống trong các mảng thưa thớt hoạt động giống như undefined
.Nếu một tham 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 }
8 được cung cấp cho 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 }
7, nó sẽ được sử dụng làm 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 bên trong mỗi lần gọi 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. Nếu nó không được cung cấp, thì undefined
được sử dụng.Phương pháp
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 }
7 không làm biến đổi mảng mà nó được gọi, nhưng hàm được cung cấp cho 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ể. Nếu vậy, các yếu tố được xử lý bởi 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 }
7 được đặt trước khi 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ỳ yếu tố nào được thêm vào mảng sau 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 }
7 bắt đầ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 }
- Các yếu tố được gán cho các chỉ mục đã được truy cập hoặc các chỉ mục bên ngoài phạm vi, sẽ không được truy cập bởi
3.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 một phần tử hiện có, chưa được liên kết của mảng được thay đổi bởi
3, giá trị của nó được chuyể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ểmconst 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 }
7 truy cập chỉ mục của phần tử đó.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 }
- Các yếu tố là
5 vẫn được truy cập.// Declare array with no elements at indexes 2, 3, and 4 const array = [0, 1, , , , 5, 6]; // Shows all indexes, not just those with assigned values array.find[[value, index] => { console.log["Visited index ", index, " with value ", value]; }]; // Visited index 0 with value 0 // Visited index 1 with value 1 // Visited index 2 with value undefined // Visited index 3 with value undefined // Visited index 4 with value undefined // Visited index 5 with value 5 // Visited index 6 with value 6 // Shows all indexes, including deleted array.find[[value, index] => { // Delete element 5 on first iteration if [index === 0] { console.log["Deleting array[5] with value ", array[5]]; delete array[5]; } // Element 5 is still visited even though deleted console.log["Visited index ", index, " with value ", value]; }]; // Deleting array[5] with value 5 // Visited index 0 with value 0 // Visited index 1 with value 1 // Visited index 2 with value undefined // Visited index 3 with value undefined // Visited index 4 with value undefined // Visited index 5 with value undefined // Visited index 6 with value 6
CẢNH BÁO: Sửa đổi đồng thời loại được mô tả trong đoạn trước thường xuyên dẫn đến mã khó hiểu và thường phải tránh [ngoại trừ trong các trường hợp đặc biệt]. Concurrent modification of the kind described in the previous paragraph frequently leads to hard-to-understand code and is generally to be avoided [except in special cases].
Phương pháp find[]
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 sẽ có thuộc tính // Declare array with no elements at indexes 2, 3, and 4
const array = [0, 1, , , , 5, 6];
// Shows all indexes, not just those with assigned values
array.find[[value, index] => {
console.log["Visited index ", index, " with value ", value];
}];
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value 5
// Visited index 6 with value 6
// Shows all indexes, including deleted
array.find[[value, index] => {
// Delete element 5 on first iteration
if [index === 0] {
console.log["Deleting array[5] with value ", array[5]];
delete array[5];
}
// Element 5 is still visited even though deleted
console.log["Visited index ", index, " with value ", value];
}];
// Deleting array[5] with value 5
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value undefined
// Visited index 6 with value 6
8 và các thuộc tính được khóa.Ví dụ
Tìm một đối tượng trong một mảng bởi 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
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
const result = inventory.find[[{ name }] => name === "cherries"];
console.log[result]; // { name: 'cherries', quantity: 5 }
Tìm một số nguyên tố trong một mảng
Ví dụ sau tìm thấy một phần tử trong mảng là số nguyên tố [hoặc trả về undefined
nếu không có số nguyên tố]:
function isPrime[element, index, array] {
let start = 2;
while [start {
console.log["Visited index ", index, " with value ", value];
}];
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value 5
// Visited index 6 with value 6
// Shows all indexes, including deleted
array.find[[value, index] => {
// Delete element 5 on first iteration
if [index === 0] {
console.log["Deleting array[5] with value ", array[5]];
delete array[5];
}
// Element 5 is still visited even though deleted
console.log["Visited index ", index, " with value ", value];
}];
// Deleting array[5] with value 5
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value undefined
// Visited index 6 with value 6
Gọi Find [] trên các đối tượng không phải là
Phương thức find[]
đọc thuộc tính
// Declare array with no elements at indexes 2, 3, and 4
const array = [0, 1, , , , 5, 6];
// Shows all indexes, not just those with assigned values
array.find[[value, index] => {
console.log["Visited index ", index, " with value ", value];
}];
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value 5
// Visited index 6 with value 6
// Shows all indexes, including deleted
array.find[[value, index] => {
// Delete element 5 on first iteration
if [index === 0] {
console.log["Deleting array[5] with value ", array[5]];
delete array[5];
}
// Element 5 is still visited even though deleted
console.log["Visited index ", index, " with value ", value];
}];
// Deleting array[5] with value 5
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value undefined
// Visited index 6 with value 6
8 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 vào từng chỉ mục số nguyên.const arrayLike = {
length: 3,
0: 2,
1: 7.3,
2: 4,
};
console.log[
Array.prototype.find.call[arrayLike, [x] => !Number.isInteger[x]],
]; // 7.3
Thông số kỹ thuật
Thông số kỹ thuật ngôn ngữ Ecmascript # sec-array.prototype.find # sec-array.prototype.find |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt