js
判
断
数
组
包
含
某
个
值
js
判
断
数
组
包
含
某
个
值
简
介
在
JavaScript
中
,
当
处
理
数
组
时
,
需
要
检
查
特
定
的
值
是
否
存
在
于
其
中
是
很
常
⻅
的
操
作
。
这
是
每
个
开
发
⼈
员
都
必
须
掌
握
的
⼀
项
基
本
操
作
。
在
本
⽂
中
,
我
们
将
探
讨
不
同
的
⽅
法
来
确
定
数
组
是
否
包
含
特
定
的
值
。
使
⽤
indexOf()
检
查
数
组
是
否
包
含
值
的
最
简
单
⽅
法
之⼀
是
使
⽤
indexOf()
⽅
法
。
该
⽅
法
在
数
组
中
搜
索
指
定
的
值
,
并
返
回
该
值
的
第
⼀个
出
现
的
索
引
,
如
果
未
找
到
该
值
,
则
返
回
-1
。
以
下
是
⼀个
⽰
例
:
const array = [1, 2, 3, 4, 5];
const value = 3;
if (array.indexOf(value) !== -1) {
console.log("The array contains the value " + value);
} else {
console.log("The array does not contain the value " + value);
}
在
这
个
例
⼦
中
,
我
们
有
⼀个
整数数
组
,
我
们
想
要
检
查
它
是
否
包
含
值
为
3
。
我
们
使
⽤
indexOf()
⽅
法
搜
索
数
组
,
如
果
结
果
不
是
-1
,
则
我
们
知
道
该
值
存
在
于
数
组
中
。
使
⽤
includes()
includes()
⽅
法
是
JavaScript
的
⼀个
新
功
能
,
提
供
了
⼀
种
更
简
单
的
⽅
法
来
检
查
数
组
是
否
包
含
值
。
该
⽅
法
返
回
⼀个
布
尔
值
,
指
⽰
数
组
中
是
否
找
到
了
指
定
的
值
。
以
下
是
⼀个
⽰
例
:
const array = [1, 2, 3, 4, 5];
const value = 3;
if (array.includes(value)) {
console.log("The array contains the value " + value);
} else {
console.log("The array does not contain the value " + value);
}