JavaScript 中的数组 some() 函数

javascriptweb developmentobject oriented programming更新于 2025/12/3 0:44:17

JavaScript 数组 some() 函数检查数组中的某些元素是否通过了给定的测试。该测试基本上是一个针对数组的每个元素运行的函数。

以下是数组 some() 方法的代码 −

示例

<!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;
   }
   .sample {
      font-size: 20px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>JavaScript Array some()</h1>
<div class="sample"></div>
<button class="btn">点击此处</button>
<h3>
Click on the above button to see if some multiples of 11 are present in
array or not
</h3>
<script>
   function checkMultiple(ele) {
      if (ele % 11 == 0) return true;
   }
   let fillEle = document.querySelector(".sample");
   let arr = [22, 19, 55, 12, 14];
   fillEle.innerHTML = arr;
   document.querySelector(".Btn").addEventListener("click", () => {
      fillEle.innerHTML = "Multiples of 11 present: " + arr.some(checkMultiple);
   });
</script>
</body>
</html>

输出

点击“CLICK HERE”按钮 −


相关文章


有用资源