JavaScript 中的数组 findIndex() 函数
javascriptweb developmentobject oriented programming更新于 2025/12/1 16:12:17
JavaScript 中的 findIndex() 函数返回数组中满足给定条件的第一个元素值的索引。
以下是数组 find() 函数的代码 −
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.findIndex {
font-size: 20px;
font-weight: 500;
}
</style>
</head>
<body>
<h1>JavaScript Array FindIndex() example</h1>
<div class="findIndex"></div>
<button class="findArr">CLICK HERE</button>
<h3>Click on the above button to get the index of lion in the array</h3>
<script>
function findLion(animal) {
return animal === "lion";
}
let fillEle = document.querySelector(".findIndex");
let arr = ["cow", "lion", "bull", "tiger"];
fillEle.innerHTML = arr;
document.querySelector(".findArr").addEventListener("click", () => {
fillEle.innerHTML = arr.findIndex(findLion);
});
</script>
</body>
</html>
输出


相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

