zoukankan      html  css  js  c++  java
  • js判断一个数组是否包含一个指定的值

    includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。

    const array1 = [1, 2, 3];
    
    console.log(array1.includes(2));
    // expected output: true
    
    const pets = ['cat', 'dog', 'bat'];
    
    console.log(pets.includes('cat'));
    // expected output: true
    
    console.log(pets.includes('at'));
    // expected output: false
    

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

    1. array.indexOf

    > 判断数组中是否存在某个值,如果存在返回数组元素的下标,否则返回-1
    
    ```
    let arr = ['something', 'anything', 'nothing', 'anything'];
    let index = arr.indexOf('nothing');
    # 结果:2
    
    ```
    

    2. array.includes(searchElement[, fromIndex])

    • 判断一个数组是否包含一个指定的值,如果存在返回 true,否则返回false。
    • 参数:searchElement需要查找的元素值。
    > 参数:thisArg(可选)从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。
    
    ```
    let numbers = [12, 5, 8, 130, 44];
    let result = numbers.includes(8);
    # 结果: true
    result = numbers.includes(118);
    # 结果: false
    
    ```
    

    3. array.find(callback[, thisArg])

    • 返回数组中满足条件的第一个元素的值,如果没有,返回undefined
    • 参数:callbackelement 当前遍历到的元素。index 当前遍历到的索引。array 数组本身。
    > 参数:thisArg(可选)指定 callback 的 this 参数。
    
    ```
    // ---------- 元素是普通字面值 ----------
    let numbers = [12, 5, 8, 130, 44];
    let result = numbers.find(item => {
        return item > 8;
    });
    # 结果: 12
    // ---------- 元素是对象 ----------
    let items = [
        {id: 1, name: 'something'},
        {id: 2, name: 'anything'},
        {id: 3, name: 'nothing'},
        {id: 4, name: 'anything'}
    ];
    let item = items.find(item => {
        return item.id == 3;
    });
    # 结果: Object { id: 3, name: "nothing" }
    ```
    

    4. array.findIndex(callback[, thisArg])

    • 返回数组中满足条件的第一个元素的索引(下标), 如果没有找到,返回-1
    • 参数:callbackelement 当前遍历到的元素。index 当前遍历到的索引。array 数组本身。
    > 参数:thisArg(可选)指定 callback 的 this 参数。
    
    ```
    // ---------- 元素是普通字面值 ----------
    let numbers = [12, 5, 8, 130, 44];
    let result = numbers.findIndex(item => {
        return item > 8;
    });
    # 结果: 0
    // ---------- 元素是对象 ----------
    let items = [
        {id: 1, name: 'something'},
        {id: 2, name: 'anything'},
        {id: 3, name: 'nothing'},
        {id: 4, name: 'anything'}
    ];
    let index = items.findIndex(item => {
        return item.id == 3;
    });
    # 结果: 2
    ```
    -------------------------------------------------------------------------
    ## 极客时间全网最便宜最优惠购买方式,优惠券返现 百度网盘 微信关注公众号“选门好课”
    扫描下方二维码关注我的公众号"选门好课",与我一起交流知识
  • 相关阅读:
    佳佳的 Fibonacci
    毒瘤之神的考验
    An error occurred while searching for implementations of method
    eclipse 开发 scala
    hbase的数据模型
    Hbase和RDBMS(关系数据库管理系统)区别
    hbase和mapreduce开发 WordCount
    使用eclipse开发hbase程序
    hbase 的体系结构
    hbase 遇到过的问题
  • 原文地址:https://www.cnblogs.com/singworld/p/13533826.html
Copyright © 2011-2022 走看看