boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

js 怎样用includes判断数组是否包含某元素


avatar
站长 2025年8月12日 4

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

js 怎样用includes判断数组是否包含某元素

核心在于

includes()

方法,它简洁明了地告诉你一个数组里是否藏着你想要找的东西。

解决方案:

includes()

方法是 JavaScript 数组的一个内置函数,用于检查数组是否包含某个指定的值。如果包含,返回

true

;否则,返回

false

。这比自己写循环去遍历数组要方便多了,而且可读性也更好。

const myArray = [1, 2, 3, 'apple', 'banana'];  // 检查数组是否包含数字 3 const hasThree = myArray.includes(3); // true  // 检查数组是否包含字符串 'orange' const hasOrange = myArray.includes('orange'); // false  // 检查数组是否包含 NaN const 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); // 2 const indexOfOrange = myArray.indexOf('orange'); // -1  if (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); // 2  const notFoundElement = myArray.find(element => element === 'orange'); // undefined const 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); // false const 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

参数以及稀疏数组等问题。



评论(已关闭)

评论已关闭