Appearance
Array.prototype.includes
用于判断数组中是否存在某一个项
语法
js
arr.includes(value [, fromIndex])
// TS 类型
type includes = ( value : any , fromIndex ?: number) => boolean
参数
- value
需要查询的值
- fromIndex
从formIndex索引处开始查找, 需要注意的是:
fromIndex 为负数,则查询所有的项
fromIndex 大于数组的长度, 则永远返回false(不查询)
注意
- 其跟Array.prototype.indexOf除了返回类型不同外,还有一个区别就是:Array.prototype.includes支持查询NaN类型的项
js
const arr = [1,2,3,4,NaN , null , undefined ];
console.log(arr.indexOf(NaN)) // -1
console.log(arr.includes(NaN)) // true