ví dụ 1 Tìm giá trị của phần tử đầu tiên có giá trị trên 18:
const lứa tuổi = [3, 10, 18, 20];
Kiểm tra chức năng (tuổi) {& nbsp; & nbsp; tuổi trở lại> 18;} return age > 18; }
hàm myFunction () {& nbsp; document.getEuityById ("demo"). innerHtml = ages.find (séc);} document.getElementById("demo").innerHTML = ages.find(checkAge); }
Hãy tự mình thử »
Định nghĩa và cách sử dụng Phương thức find()
trả về giá trị của phần tử đầu tiên vượt qua thử nghiệm.
Phương thức find()
thực thi một hàm cho mỗi phần tử mảng.
Phương thức find()
trả về undefined
nếu không tìm thấy các phần tử.
Phương thức find()
không thực thi hàm cho các phần tử trống.
Phương pháp find()
không thay đổi mảng gốc.
Cú pháp
mảng.find (function (currentValue, index, mảng), điều này
Thông số
hàm số() Yêu cầu.a hàm để chạy cho từng phần tử mảng. A function to run for each array element.
Hiện tại giá trị Yêu cầu. Giá trị của phần tử hiện tại. The value of the current element.
mục lục Tùy chọn. Chỉ số của phần tử hiện tại. The index of the current element.
mảng Tùy chọn. Mảng của phần tử hiện tại. The array of the current element.
giá trị này Không bắt buộc. Mặc định undefined
. Một giá trị được truyền đến hàm là 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 }
3 của nó. A value passed to the function as its 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 value.
Giá trị trả về
Loại hình Sự mô tả Một giá trị Giá trị của phần tử đầu tiên vượt qua bài kiểm tra. Nếu không, nó trả về undefined
. Otherwise it returns undefined
.
Ví dụ 2 Tìm giá trị của phần tử đầu tiên có giá trị trên một số cụ thể:
Thử nó
const lứa tuổi = [4, 12, 16, 20]; const ages = [4, 12, 16, 20];
Kiểm tra chức năng (tuổi) {& nbsp; Trả về tuổi> document.getEuityById ("Agetocheck"). Giá trị;} return age > document.getElementById("ageToCheck").value; }
function myFunction () document.getElementById("demo").innerHTML = ages.find(checkAge); }
Hãy tự mình thử »
Định nghĩa và cách sử dụng Phương thức find()
trả về giá trị của phần tử đầu tiên vượt qua thử nghiệm.
Phương thức find()
thực thi một hàm cho mỗi phần tử mảng.
Phương thức find()
trả về undefined
nếu không tìm thấy các phần tử. Phương thức find()
không thực thi hàm cho các phần tử trống. Phương pháp find()
không thay đổi mảng gốc. Cú pháp mảng.find (function (currentValue, index, mảng), điều này Thông số Thông số Thông số Thông số Thông số
hàm số()
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ó
Nếu bạn cần chỉ số của phần tử tìm thấy trong mảng, hãy sử 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 }
9.index of the found element in the 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 }
9. Nếu bạn cần tìm chỉ mục của một giá trị, hãy sử dụng 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 }
0. (Nó 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 }
9, nhưng kiểm tra từng phần tử cho sự bình đẳng với giá trị thay vì sử dụng chức năng kiểm tra.)index of a value , use
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 }
0. (It's similar to 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, but checks each element for equality with the value instead of using a testing function.) Nếu bạn cần tìm nếu một giá trị tồn tại trong một mảng, hãy sử dụng 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 }
2. 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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
2. Again, it checks each element for equality with the value instead of using a testing function. Nếu bạn cần tìm nếu bất kỳ phần tử nào thỏa mãn chức năng kiểm tra được cung cấp, hãy sử dụng 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 }
3.
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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
4Chứ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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
5Phần tử hiện tại trong mảng.
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 }
6Chỉ 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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
7Mảng mà
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 }
8 đã đượ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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
9 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 }
3 bên trong 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 }
4.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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
8 thực thi hàm 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 }
4 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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
4 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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
8 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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
8 trả về undefined
.
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 }
4 đượ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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
9 được cung cấp cho 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 }
8, 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 }
3 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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
4. 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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
8 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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
4 có thể. Nếu vậy, các phần tử được xử lý bởi 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 }
8 đượ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 } ,
] ;
const result = inventory. find ( ( { name } ) => name === "cherries" ) ;
console. log ( result) ; // { name: 'cherries', quantity: 5 }
4. Vì vậ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 }
4 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 đến 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 }
8 bắt đầu.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 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 }
4. 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 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 }
4, giá trị của nó được chuyển cho 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 }
4 sẽ là giá trị tại thời điểm 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 }
8 truy cập chỉ mục của phần tử đó. Các yếu tố là const arrayLike = {
length : 3 ,
0 : 2 ,
1 : 7.3 ,
2 : 4 ,
} ;
console. log (
Array . prototype. find . call ( arrayLike, ( x ) => ! Number. isInteger ( x) ) ,
) ; // 7.3
6 vẫn được truy cập. 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 }
3 có thuộc tính const arrayLike = {
length : 3 ,
0 : 2 ,
1 : 7.3 ,
2 : 4 ,
} ;
console. log (
Array . prototype. find . call ( arrayLike, ( x ) => ! Number. isInteger ( x) ) ,
) ; // 7.3
9 và các thuộc tính được khóa.
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 <= Math. sqrt ( element) ) {
if ( element % start++ < 1 ) {
return false ;
}
}
return element > 1 ;
}
console. log ( [ 4 , 6 , 8 , 12 ] . find ( isPrime) ) ; // undefined, not found
console. log ( [ 4 , 5 , 8 , 12 ] . find ( isPrime) ) ; // 5
Sử dụng Find () trên các mảng thưa thớt
Các khe trống trong các mảng thưa thớt được truy cập và được đối xử giống như undefined
.
// 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
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
const arrayLike = {
length : 3 ,
0 : 2 ,
1 : 7.3 ,
2 : 4 ,
} ;
console. log (
Array . prototype. find . call ( arrayLike, ( x ) => ! Number. isInteger ( x) ) ,
) ; // 7.3
9 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à 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
Sự chỉ rõ 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
Làm thế nào để bạn tìm kiếm một mảng các đối tượng?
Tìm kiếm trong một mảng các đối tượng có thể được thực hiện trong JavaScript bằng các phương thức vòng lặp, mảng.find () hoặc mảng.findIndex ().using a loop, Array. find() or Array. findIndex() methods .
Làm cách nào để tìm một phần tử trong một mảng mảng?
Sử dụng foreach () để tìm một phần tử trong một mảng mảng.nguyên mẫu.Phương thức foreach () thực thi cùng một mã cho từng phần tử của một mảng. to find an element in an array
The Array. prototype. forEach() method executes the same code for each element of an array.
Làm thế nào để Array tìm thấy công việc trong JavaScript?
JavaScript Array find () Phương thức Find () trả về giá trị của phần tử đầu tiên vượt qua thử nghiệm.Phương thức Find () thực thi một hàm cho mỗi phần tử mảng.Phương thức Find () trả về không xác định nếu không tìm thấy các phần tử.The find() method returns the value of the first element that passes a test . The find() method executes a function for each array element. The find() method returns undefined if no elements are found.
Bạn có thể lập chỉ mục một mảng trong JavaScript không?
Một mục trong mảng JavaScript được truy cập bằng cách tham khảo số chỉ mục của mục trong dấu ngoặc vuông.Chúng tôi biết 0 sẽ luôn xuất ra mục đầu tiên trong một mảng.Chúng ta cũng có thể tìm thấy mục cuối cùng trong một mảng bằng cách thực hiện một thao tác trên thuộc tính độ dài và áp dụng nó dưới dạng số chỉ mục mới. . We know 0 will always output the first item in an array. We can also find the last item in an array by performing an operation on the length property and applying that as the new index number.