方法一:
let arr = [1,2,3,4];
if (arr.indexOf(2) != -1){
console.log("数组含有2")
}else {
console.log("数组不含2")
}
方法二:
let arr = [1,2,3,4];
for (let i = 0;i < arr.length;i++){
if (arr[i] === 2){
console.log("数组含有2")
}
}
方法三:
let arr = [1,2,3,4];
arr.find(value => {
if (value === 2){
console.log("数组含有2")
}
})
方法四:arr.includes() ES6新方法,返回true或false
let arr = [1,2,3,4];
if(arr.includes(2)){
console.log("数组含有2");
}else{
console.log("数组不含2");
}