find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回undefined。 1 2 3 4 5 functionisBigEnough(element) { returnelement >= 15; } [12, 5, 8, 130, 44].find(isBigEnough);// 130 另请参见findIndex()方法,它返回数组中找到的元素的索引,而不是其值。 如果你需要找到一个元素的...
for...in 成功 跳出本次循环 不合法 不合法 不合法 √ Array.map() 不合法 不合法 跳出本次循环 跳出本次循环 跳出本次循环 × Array.some() 不合法 不合法 跳出本次循环 成功 跳出本次循环 √ Array.every() 不合法 不合法 成功 跳出本次循环 成功 √ Array.find() 不合法 不合法 跳出本次循环 成...
function isPrime(element, index, array) { var 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(is...
使用汽车示例,让我们使用该.find()方法获得数组中遇到的第一辆昂贵的汽车。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 constcars=[{brand:"Porsche",price:100000},{brand:"Audi",price:80000},{brand:"Toyota",price:30000}];constexpensiveCar=cars.find(car=>car.price>=40000);// Result - ...
代码语言:javascript 复制 constarr=[1,2,3,4,5];constresult=$(arr).find((index,value)=>value>3);console.log(result);// [4, 5] inArray inArray方法用于检查一个元素是否存在于数组中。它接受两个参数:需要查找的元素和可选的起始索引。如果找到该元素,则返回其索引;如果未找到,则返回-1。
The find() method returns the value of the first element in an array that pass a test (provided as a function).The find() method executes the function once for each element present in the array:If it finds an array element where the function returns a true value, find() returns the ...
JavaScript Array find() ❮PreviousJavaScript ArrayReferenceNext❯ Example 1 Find the value of the first element with a value over 18: constages = [3,10,18,20]; functioncheckAge(age) { returnage >18; } functionmyFunction() { document.getElementById("demo").innerHTML= ages.find(check...
The findIndex() method returns the index of the first element in an array that pass a test (provided as a function).The findIndex() method executes the function once for each element present in the array:If it finds an array element where the function returns a true value, findIndex()...
❮PreviousJavaScript ArrayReferenceNext❯ Example Get the array constructor: constfruits = ["Banana","Orange","Apple","Mango"]; lettext = fruits.constructor; Try it Yourself » Description Theconstructorproperty returns the function that created the Array prototype. ...
We can find the length of an array using the length property. For example, const dailyActivities = [ "eat", "sleep"]; // return the length of array console.log(dailyActivities.length); // Output: 2 Run Code Relationship between arrays and objects in JavaScript. In JavaScript, arrays...