includes() 方法用于判断数组是否包含指定元素,返回 true 或 false;2. 其他方法包括 indexof()(返回索引,不存在则为-1)、find()/findindex()(通过回调函数查找);3. 使用 includes() 时需注意:使用严格相等比较(类型必须匹配)、能正确处理 nan、fromindex 参数影响搜索起始位置、稀疏数组中的空槽被视为 undefined、在旧浏览器中可能存在兼容性问题。

核心在于
includes()
方法,它简洁明了地告诉你一个数组里是否藏着你想要找的东西。
解决方案:
includes()
方法是 JavaScript 数组的一个内置函数,用于检查数组是否包含某个指定的值。如果包含,返回
true
;否则,返回
false
。这比自己写循环去遍历数组要方便多了,而且可读性也更好。
const myArray = [1, 2, 3, 'apple', 'banana'];// 检查数组是否包含数字 3const hasThree = myArray.includes(3); // true// 检查数组是否包含字符串 'orange'const hasOrange = myArray.includes('orange'); // false// 检查数组是否包含 NaNconst hasNaN = myArray.includes(NaN); // 注意:includes() 可以正确处理 NaN
它不仅可以查找基本类型,还可以查找字符串,甚至
NaN
。 注意,
includes()
使用的是严格相等(===)来比较,所以类型也必须匹配。
includes()
的基本语法是
array.includes(searchElement, fromIndex)
。
searchElement
是你要查找的元素,
fromIndex
是可选的,表示从哪个索引位置开始查找。 如果不指定
fromIndex
,默认从数组的开头开始查找。
JS数组中除了includes还有哪些方法可以判断数组是否包含某个元素?
除了
includes()
,还有
indexOf()
和
find()
/
findIndex()
可以用来判断数组是否包含某个元素,不过它们的行为和用途略有不同。
indexOf()
:
indexOf()
方法返回数组中找到指定元素的第一个索引。 如果数组中不存在该元素,则返回 -1。 这也是一种常见的判断数组是否包含某个元素的方法。
const myArray = [1, 2, 3, 'apple', 'banana'];const indexOfThree = myArray.indexOf(3); // 2const indexOfOrange = myArray.indexOf('orange'); // -1if (myArray.indexOf('apple') !== -1) { console.log('数组包含 apple');}
indexOf()
的缺点是它不能检测
NaN
,因为
NaN !== NaN
。 另外,它只返回找到的第一个元素的索引,如果你需要知道元素出现的所有位置,就需要自己写循环了。
find()
和
findIndex()
:
find()
方法返回数组中满足提供的测试函数的第一个元素的值。
findIndex()
方法返回数组中满足提供的测试函数的第一个元素的索引。 如果没有找到满足条件的元素,
find()
返回
undefined
,
findIndex()
返回 -1。
const myArray = [1, 2, 3, 'apple', 'banana'];const foundElement = myArray.find(element => element === 'apple'); // 'apple'const foundIndex = myArray.findIndex(element => element === 3); // 2const notFoundElement = myArray.find(element => element === 'orange'); // undefinedconst notFoundIndex = myArray.findIndex(element => element === 'orange'); // -1
find()
和
findIndex()
更加灵活,因为你可以使用自定义的测试函数。 比如,你可以查找数组中大于 2 的第一个数字。
const myArray = [1, 2, 3, 4, 5];const found = myArray.find(element => element > 2); // 3
不过,如果只是简单地判断数组是否包含某个元素,
includes()
通常是最简洁的选择。
使用 includes() 方法时有哪些需要注意的点?
类型匹配:
includes()
使用严格相等(===)进行比较,这意味着类型必须匹配。 例如,
includes(3)
不会匹配字符串
'3'
。
const myArray = [1, 2, '3'];const hasThreeNumber = myArray.includes(3); // falseconst hasThreeString = myArray.includes('3'); // true
NaN
的处理:
includes()
可以正确处理
NaN
,这与其他一些方法(如
indexOf()
)不同。
const myArray = [1, 2, NaN];const hasNaN = myArray.includes(NaN); // true
fromIndex
参数: 可以使用
fromIndex
参数指定开始搜索的位置。 如果
fromIndex
大于或等于数组的长度,则返回
false
,不会搜索数组。 如果
fromIndex
是负数,则从
array.length + fromIndex
的索引开始搜索。
const myArray = [1, 2, 3, 4, 5];const hasOneFromIndexTwo = myArray.includes(1, 2); // false (从索引 2 开始搜索)const hasFourFromIndexNegativeTwo = myArray.includes(4, -2); // true (从索引 3 开始搜索)
稀疏数组:
includes()
在稀疏数组中的行为可能与预期不同。 稀疏数组是包含空槽的数组。
includes()
不会跳过空槽,而是将其视为
undefined
。
const myArray = [1, , 3]; // 注意中间有一个空槽const hasUndefined = myArray.includes(undefined); // true (因为空槽被视为 undefined)
浏览器兼容性:
includes()
方法是 ES2016 (ES7) 引入的,因此在一些旧版本的浏览器中可能不支持。 如果需要兼容旧版本浏览器,可以使用
indexOf()
或使用 Babel 等工具进行代码转换。
总的来说,
includes()
是一个简单易用且功能强大的方法,但在使用时需要注意类型匹配、
NaN
的处理、
fromIndex
参数以及稀疏数组等问题。
以上就是js 怎样用includes判断数组是否包含某元素的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1514465.html
微信扫一扫
支付宝扫一扫