// 检查数组是否包含某个元素可以使用 Array.prototype.includes() 方法
const array = [1, 2, 3, 4, 5];
// 检查数组中是否包含数字 3
if (array.includes(3)) {
console.log('数组中包含数字 3');
} else {
console.log('数组中不包含数字 3');
}
// 检查数组中是否包含数字 6
if (array.includes(6)) {
console.log('数组中包含数字 6');
} else {
console.log('数组中不包含数字 6');
}
Array.prototype.includes()
方法用于判断数组是否包含某个指定的值,根据情况返回 true
或 false
。array
,然后分别检查数组中是否包含数字 3
和 6
,并输出相应的结果。上一篇:js 数组方法
下一篇:js 爬虫
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站