JavaScript 数组includes() 函数
JavaScript 的 array.includes() 方法用于检查一个数组是否包含某个指定的元素。
语法如下 −
array.includes(ele, start)
上面,参数 ele 是要搜索的元素。start 参数是搜索开始的位置。
现在让我们在 JavaScript 中实现 array.includes() 方法 −
示例
<!DOCTYPE html> <html> <body> <h2>Car Variant</h2> <button onclick="display()">Result</button> <p id="test"></p> <script> var carid = ["110", "230", "299", "399"]; document.getElementById("test").innerHTML = carid; function display() { var n = carid.includes("230"); document.getElementById("test").innerHTML = "The array has the specific value 230? = "+n; } </script> </body> </html>
输出
上面,点击“结果”按钮 −
示例
<!DOCTYPE html> <html> <body> <h2>Car Variant</h2> <p>Does the array includes the specific element Crossover?</p> <button onclick="display()">Result</button> <p id="test"></p> <script> var car = ["Hatchback", "Convertible"]; var res2 = car.entries(); for (val of res2) { document.getElementById("test").innerHTML += val + "<br>"; } function display() { var res = car.includes("Crossover"); document.getElementById("test").innerHTML = res; } </script> </body> </html>
输出
点击上面的“结果”按钮 −
广告