相当于自己实现include()方法
function judge(array,item) {//定义函数传入
for (let index = 0; index <array.length; index++) {//遍历数组
if(item==array[index]){
return true; //包含则返回true
}
}
return false;//不包含返回false
}
let arr2 = [8,'hello',10,8,8,"hello","abc",10,5];//创建数组
console.log(judge(arr2,10)); //输出10
文章展示了如何在JavaScript中定义一个函数`judge`,该函数用于检查一个数组是否包含特定的项。通过遍历数组并比较元素,函数返回布尔值来表示目标项是否存在。示例中,函数被应用于数组`arr2`,检查是否包含数字10。

被折叠的 条评论
为什么被折叠?



